AsyncIterator
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.
AnAsyncIterator
object is an object that conforms to theasync iterator protocol by providing anext()
method that returns a promise fulfilling to an iterator result object. TheAsyncIterator.prototype
object is a hidden global object that all built-in async iterators inherit from. It provides an[Symbol.asyncIterator]()
method that returns the async iterator object itself, making the async iterator alsoasync iterable.
Note thatAsyncIterator
isnot a global object, although it will be in the future with theasync iterator helpers proposal. TheAsyncIterator.prototype
object shared by all built-in async iterators can be obtained with the following code:
const AsyncIteratorPrototype = Object.getPrototypeOf( Object.getPrototypeOf(Object.getPrototypeOf((async function* () {})())),);
Description
Currently, the only built-in JavaScript async iterator is theAsyncGenerator
object returned byasync generator functions. There are some other built-in async iterators in web API, such as the one of aReadableStream
.
Each of these async iterators have a distinct prototype object, which defines thenext()
method used by the particular async iterator. All of these prototype objects inherit fromAsyncIterator.prototype
, which provides an[Symbol.asyncIterator]()
method that returns the async iterator object itself, making the async iterator alsoasync iterable.
Note:AsyncIterator.prototype
does not implement[Symbol.iterator]()
, so async iterators are notsync iterable by default.
Instance methods
AsyncIterator.prototype[Symbol.asyncIterator]()
Returns the async iterator object itself. This allows async iterator objects to also be async iterable.
Examples
Using an async iterator as an async iterable
All built-in async iterators are also async iterable, so you can use them in afor await...of
loop:
const asyncIterator = (async function* () { yield 1; yield 2; yield 3;})();(async () => { for await (const value of asyncIterator) { console.log(value); }})();// Logs: 1, 2, 3
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-asynciteratorprototype |