W3cubDocs

/JavaScript

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]()

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 @@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

Browser compatibility

Desktop Mobile Server
Chrome Edge Firefox Opera Safari Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet WebView Android Deno Node.js
@@asyncIterator 63 79 57 50 11.1 63 57 46 11.3 8.0 63 1.0 10.0.0

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncIterator/@@asyncIterator