Async Iterators
suggest changeIntroduction
An async function returns a promise. await yields to the caller until that promise settles, then continues with the result or throws if the promise rejects.
An iterator lets a collection be looped through with for...of.
An async iterator's next() method returns a promise for each result. Consume it with for await...of.
Async iteration has been part of the language since ECMAScript 2018. The earlier proposal describes the design; it is not a stage 3 draft anymore.
Syntax
async function* asyncGenerator() {
yield await asyncOperationWhichReturnsAPromise();
}
for await (const result of asyncGenerator()) {
// result is the fulfilled value
}
Remarks
An async iterator is a declarative pull stream as opposed to an Observable’s declarative push stream.
Useful Links
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents