我有一个看起来像这样的循环:
newThreadIds.map(async function(id) {
let thread = await API.getThread(id);
await ActiveThread.findOneAndUpdate({number: id}, {posts: thread.posts}, {upsert: true}).exec();
await Q.delay(1000);
});
问题是每次迭代都是异步执行的,我希望它们之间有 1 秒的延迟。我知道如何用 promises 来做,但它看起来很难看,我更愿意用 async/await 和尽可能少的嵌套来做。
最佳答案
map
函数不知道它的回调是异步的并返回一个 promise 。它只是立即遍历数组并创建一个 promise 数组。你会像这样使用它
const promises = newThreadIds.map(async function(id) {
const thread = await API.getThread(id);
return ActiveThread.findOneAndUpdate({number: id}, {posts: thread.posts}, {upsert: true}).exec();
});
const results = await Promise.all(promises);
await Q.delay(1000);
对于顺序执行,您需要使用 Bluebird's mapSeries
function (或来自您各自库的类似内容),它关心每次迭代的 promise 返回值。
在纯 ES6 中,您必须使用一个实际的循环,其控制流将遵循循环体中的 await
关键字:
let results = [];
for (const id of newThreadIds) {
const thread = await API.getThread(id);
results.push(await ActiveThread.findOneAndUpdate({number: id}, {posts: thread.posts}, {upsert: true}).exec());
await Q.delay(1000);
}
关于javascript - 使异步/等待循环按顺序执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35632531/