根据 MDN Web 文档:
There are currently no built-in JavaScript objects that have the [Symbol.asyncIterator] key set by default.
我的问题是这是如何起作用的:
function promises() {
return [
new Promise(x => setTimeout(() => x(1), 1000)),
new Promise(x => setTimeout(() => x(2), 2000)),
new Promise(x => setTimeout(() => x(3), 3000)),
]
}
for await (x of promises()) console.log(x)
最佳答案
MDN 还指出
The
for await...of
statement creates a loop iterating over async iterable objects as well as on sync iterables, including: built-in String, Array, Array-like objects (e.g., arguments or NodeList), TypedArray, Map, Set, and user-defined async/sync iterables - MDN (emphasis mine)
关于javascript - 数组中内置Symbol.asyncIterator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58251194/