AsyncIterator.prototype[@@asyncIterator]()
The [@@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.
Try it
Syntax
js
asyncIterator[Symbol.asyncIterator]()
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 @@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.
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 Language Specification # sec-asynciteratorprototype-asynciterator |
Browser compatibility
BCD tables only load in the browser