docs.rodeo

MDN Web Docs mirror

AsyncGenerator

{{JSRef}} 

The AsyncGenerator object is returned by an {{jsxref("Statements/async_function*", "async generator function", "", 1)}}  and it conforms to both the async iterable protocol and the async iterator protocol.

Async generator methods always yield {{jsxref("Promise")}}  objects.

AsyncGenerator is a subclass of the hidden {{jsxref("AsyncIterator")}}  class.

{{InteractiveExample("JavaScript Demo: Expressions - Async Function Asterisk", "taller")}} 

async function* foo() {
  yield await Promise.resolve("a");
  yield await Promise.resolve("b");
  yield await Promise.resolve("c");
}

let str = "";

async function generate() {
  for await (const val of foo()) {
    str = str + val;
  }
  console.log(str);
}

generate();
// Expected output: "abc"

Constructor

There’s no JavaScript entity that corresponds to the AsyncGenerator constructor. Instances of AsyncGenerator must be returned from async generator functions:

async function* createAsyncGenerator() {
  yield Promise.resolve(1);
  yield await Promise.resolve(2);
  yield 3;
}
const asyncGen = createAsyncGenerator();
asyncGen.next().then((res) => console.log(res.value)); // 1
asyncGen.next().then((res) => console.log(res.value)); // 2
asyncGen.next().then((res) => console.log(res.value)); // 3

There’s only a hidden object which is the prototype object shared by all objects created by async generator functions. This object is often stylized as AsyncGenerator.prototype to make it look like a class, but it should be more appropriately called {{jsxref("AsyncGeneratorFunction.prototype.prototype")}} , because AsyncGeneratorFunction is an actual JavaScript entity. To understand the prototype chain of AsyncGenerator instances, see {{jsxref("AsyncGeneratorFunction.prototype.prototype")}} .

Instance properties

These properties are defined on AsyncGenerator.prototype and shared by all AsyncGenerator instances.

Instance methods

Also inherits instance methods from its parent {{jsxref("AsyncIterator")}} .

Examples

Async generator iteration

The following example iterates over an async generator, logging values 1–6 to the console at decreasing time intervals. Notice how each time a Promise is yielded, but it’s automatically resolved within the for await...of loop.

// An async task. Pretend it's doing something more useful
// in practice.
function delayedValue(time, value) {
  return new Promise((resolve /*, reject*/) => {
    setTimeout(() => resolve(value), time);
  });
}

async function* generate() {
  yield delayedValue(2000, 1);
  yield delayedValue(1000, 2);
  yield delayedValue(500, 3);
  yield delayedValue(250, 4);
  yield delayedValue(125, 5);
  yield delayedValue(50, 6);
  console.log("All done!");
}

async function main() {
  for await (const value of generate()) {
    console.log("value", value);
  }
}

main().catch((e) => console.error(e));

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN