【发布时间】:2020-01-01 02:05:01
【问题描述】:
我需要在循环中的上一个请求结束后每次发送一个请求。但我不能通过异步/等待来做到这一点。请帮忙。
genQuote = () => {
let quotes = [{quote: 1},{quote: 2}...{quote: n}]
quotes.map(async (simple,i) => {
await this.sendSimple(simple)
}).then(()=>{console.log('Done ' + i)}))
}
sendSimple = (simple) => {
request('rest/api', {
method: 'POST',,
body: JSON.stringify(simple)
})
}
【问题讨论】:
-
return this.sendSimple(simple);和return request('rest/api', ...)你的函数需要返回承诺等待等待做任何有趣的事情。 -
尽量不要将 async/await 与 .then().catch() 混合使用
标签: javascript promise async-await fetch