// Asynchronous iteration --> Symbol.asyncIterator async function main() { const syncIterable = [ Promise.resolve('a'), Promise.resolve('b'), ]; for await (const x of syncIterable) { console.log("x", x); // a, b } for (const s of await Promise.all(syncIterable)) { console.log("s", s); // a, b } } main();
本文通过一个简单的异步迭代示例介绍了如何使用 Symbol.asyncIterator 来遍历包含 Promise 的可迭代对象。示例中展示了两种不同的遍历方式:一种是使用 for-await-of 循环直接迭代,另一种是通过 await Promise.all() 方法将所有 Promise 转换为 resolved 状态后再进行迭代。
1562

被折叠的 条评论
为什么被折叠?



