AsyncIterator.prototype[Symbol.asyncIterator]()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.
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.
Syntax
asyncIterator[Symbol.asyncIterator]()
Parameters
None.
Return value
The value of this
, which is the async iterator object itself.
Examples
Iteration using for await...of loop
Note that you seldom need to call this method directly. The existence of the [Symbol.asyncIterator]()
method makes all built-in async iterators async iterable, and iterating syntaxes like the for await...of
loop automatically calls this method to obtain the async iterator to loop over.
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 Language Specification # sec-asynciteratorprototype-asynciterator |
Browser compatibility
BCD tables only load in the browser