AsyncIterator - JavaScript | MDN
Description
Currently, the only built-in JavaScript async iterator is the AsyncGenerator object returned by async generator functions. There are some other built-in async iterators in web API, such as the one of a ReadableStream.
Each of these async iterators have a distinct prototype object, which defines the next() method used by the particular async iterator. All of these prototype objects inherit from AsyncIterator.prototype, which provides a [Symbol.asyncIterator]() method that returns the async iterator object itself, making the async iterator also async iterable.
Instance methods
AsyncIterator.prototype[Symbol.asyncDispose]()-
Calls and awaits the
return()method ofthis, if it exists. This implements the async disposable protocol and allows it to be disposed when used withawait using. 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 a for await...of loop:
js
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® 2027 Language Specification # sec-asynciteratorprototype |