Widely availableSupported across all major browsers. Safe to use in production.

Overview

Async generator functions (async function*) create iterators that return multiple promises, one after another, on-demand.

Browser support

Feature Desktop Mobile
Chrome
Edge
Firefox
Safari
Chrome Android
Safari iOS
63
79
55
12
63
12

The next() method of AsyncGenerator instances returns the next value in the sequence.

63
79
55
12
63
12

The return() method of AsyncGenerator instances acts as if a return statement is inserted in the generator's body at the current suspended position, which finishes the generator and allows the generator to perform any cleanup tasks when combined with a try...finally block.

63
79
55
12
63
12

The throw() method of AsyncGenerator instances acts as if a throw statement is inserted in the generator's body at the current suspended position, which informs the generator of an error condition and allows it to handle the error, or perform cleanup and close itself.

63
79
55
12
63
12
Other

The AsyncGeneratorFunction object provides methods for async generator functions. In JavaScript, every async generator function is actually an AsyncGeneratorFunction object.

63
79
55
12
63
12
Built-in object

The AsyncGeneratorFunction() constructor creates AsyncGeneratorFunction objects.

63
79
55
12
63
12
Other
javascript.functions.method_definitions.async_generator_methods

Async generator methods

63
79
57
12
63
12

`async function*` expression

63
79
57
12
63
12

`async function*` statement

63
79
57
12
63
12
1+Supported (version) Not supported Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)

Syntax

JAVASCRIPT
async function* fetchPages(url) {
  let page = 1;
  while (true) {
    const res = await fetch(`${url}?page=${page}`);
    const data = await res.json();
    if (data.length === 0) return;
    yield data;
    page++;
  }
}

for await (const page of fetchPages('/api/items')) {
  console.log(page);
}

Live demo

async function* Basics

asyncgenerator definition.await and yield combinationpossible. with async function*

PreviewFullscreen

pe-jne-tion stylede-taread

pe-j per and to async in de-ta readsequentialoutputpattern.

PreviewFullscreen

Normal generator and. Difference

different. with Yield value that sync/async, side that for...of / for await...of.

PreviewFullscreen

Use cases

  • Using Async generators

    Async generator functions (async function*) create iterators that return multiple promises, one after another, on-demand.

Cautions

  • No specific concerns. Stable across all major browsers.

Accessibility

  • When updating the DOM dynamically, announce important changes to assistive technology with aria-live regions.

Powered by web-features