W3cubDocs

/JavaScript

AsyncGenerator

The AsyncGenerator object is returned by an async generator function and it conforms to both the async iterable protocol and the async iterator protocol.

Async generator methods always yield Promise objects.

Try it

Constructor

The AsyncGenerator constructor is not available globally. Instances of AsyncGenerator must be returned from async generator functions

async function* createAsyncGenerator() {
  yield await Promise.resolve(1);
  yield await Promise.resolve(2);
  yield await Promise.resolve(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

Instance methods

AsyncGenerator.prototype.next()

Returns a Promise which will be resolved with the given value yielded by the yield expression.

AsyncGenerator.prototype.return()

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.

AsyncGenerator.prototype.throw()

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.

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(100, 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

Browser compatibility

Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet Deno Node.js
AsyncGenerator
63
79
55
No
50
12
63
63
55
46
12
8.0
1.0
10.0.0
8.10.0
next
63
79
55
No
50
12
63
63
55
46
12
8.0
1.0
10.0.0
8.10.0
return
63
79
55
No
50
12
63
63
55
46
12
8.0
1.0
10.0.0
8.10.0
throw
63
79
55
No
50
12
63
63
55
46
12
8.0
1.0
10.0.0
8.10.0

See also

© 2005–2022 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/AsyncGenerator