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)
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 (eg, arguments or NodeList), TypedArray, Map, Set, and user-defined async/sync iterables - MDN (emphasis mine)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.