All topics
Asyncadvanced

Async Iterators and for-await-of

Iterating over sequences of values that arrive asynchronously, such as paginated APIs or streamed data, using async generators and for-await-of.

Regular iterators pull values synchronously, but a lot of real data doesn't arrive that way — paginated API results, streamed reads, WebSocket messages. Async iterators and for await...of extend the iteration protocol to handle this.

for await...of is like reading letters arriving one by one over several days, versus for...of reading a stack already sitting on your desk.

Key Concepts

1
An async iterator's next() method returns a Promise resolving to {value, done}. Symbol.asyncIterator is the async counterpart to Symbol.iterator, and for await...of automatically awaits each next() call.
2
Async generator functions (async function*) let you use both yield and await freely inside, producing an object that's automatically async-iterable — ideal for paginated API consumption.
3
for await...of can also consume a plain array of promises directly, awaiting each in turn, useful for ordered sequential processing rather than full concurrency via Promise.all.