Reputation: 6432
According to MDN Web Docs:
There are currently no built-in JavaScript objects that have the [Symbol.asyncIterator] key set by default.
My question is how come this is working:
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)
Upvotes: 0
Views: 590
Reputation: 6263
MDN also states that
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)
Upvotes: 1