小编Nic*_*ian的帖子

等待 VS Promise.all

这有什么区别吗:

const promises = await Promise.all(items.map(e => somethingAsync(e)));
for (const res of promises) {
  // do some calculations
}
Run Code Online (Sandbox Code Playgroud)

和这个 ?

for await (const res of items.map(e => somethingAsync(e))) {
  // do some calculations
}
Run Code Online (Sandbox Code Playgroud)

我知道在第一个片段中,所有的承诺都被同时触发,但我不确定第二个。for 循环是否等待第一次迭代完成以调用下一个 promise ?还是所有的 Promise 都是同时触发的,循环内部就像是它们的回调?

javascript node.js promise async-await for-await

24
推荐指数
3
解决办法
2万
查看次数

标签 统计

async-await ×1

for-await ×1

javascript ×1

node.js ×1

promise ×1