0

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) 
1
  • for await falls back to a synchronous iterator Commented Oct 5, 2019 at 18:52

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

5 Comments

Stupid mistake, I didn't notice the words "by default"
by default makes no difference
There's no object that comes with "asyncIterator" by default, but if you use "for await", "asyncIterator' is created.
@RafiHenig - oh, I don't see where that is documented
Because "asyncIterator" must be used in order to run the code above, and if it's not there by default then it must be created some how.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.