Async iterators and the for await..of loop
An AsyncIterator object is an object that conforms to the async iterator protocol by providing a next() method that returns a promise fulfilling to an iterator result object. The AsyncIterator.prototype object is a hidden global object that all built-in async iterators inherit from. It provides a [Symbol.asyncIterator]() method that returns the async iterator object itself, making the async iterator also async iterable.
Note that AsyncIterator is not a global object, although it will be in the future with the async iterator helpers proposal. The AsyncIterator.prototype object shared by all built-in async iterators can be obtained with the following code:
js
const AsyncIteratorPrototype = Object.getPrototypeOf( Object.getPrototypeOf(Object.getPrototypeOf((async function* () {})())), );
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 63 | 79 | 57 | 11.1 | 63 | 11.3 | |
| The [Symbol.asyncIterator]() method of AsyncIterator instances implements the async iterable protocol and allows built-in async iterators to be consumed by most syntaxes expecting async iterables, such as for await...of loops. It returns the value of this, which is the async iterator object itself. | 63 | 79 | 57 | 11.1 | 63 | 11.3 |
| The Symbol.asyncIterator static data property represents the well-known symbol Symbol.asyncIterator. The async iterable protocol looks up this symbol for the method that returns the async iterator for an object. In order for an object to be async iterable, it must have a [Symbol.asyncIterator] key. | 63 | 79 | 57 | 11.1 | 63 | 11.3 |
| Other | ||||||
| The for await...of statement creates a loop iterating over async iterable objects as well as sync iterables. This statement can only be used in contexts where await can be used, which includes inside an async function body and in a module. | 63 | 79 | 57 | 12 | 63 | 12 |
| Statement | ||||||
async iterators async iterators | 63 | 12 | 57 | 7 | 63 | 7 |
Syntax
// for await...of loop
for await (const chunk of readableStream) {
process(chunk);
} Live demo
for await...of Basics
Iterate. with Symbol.asyncIterator implementationobject for await...of.
Normal iterator and. Comparison
Symbol.iterator and Symbol.asyncIterator. Compare usage.
Use cases
-
Using Async iterators and the for await..of loop
An AsyncIterator object is an object that conforms to the async iterator protocol by providing a next() method that returns a promise fulfilling to an iterator result object.
Cautions
- No specific concerns. Stable across all major browsers.
Accessibility
- When updating the DOM dynamically, announce important changes to assistive technology with aria-live regions.