This page is the JavaScript AsyncGenerator Methods index. Every object returned by an async function* has three methods, and each row below links to a full guide with syntax, examples you can run, and FAQs.
Learn what an async generator is on /js/async-generator. Use this page when you need a specific method such as next().
All three methods return a Promise.
Each one resolves to an object { value, done }, or rejects when the generator throws an error it did not catch. Always await the call or add .then().
Syntax
How to Call an AsyncGenerator Method
Call the methods on the object an async function* returns, inside an async function (or a module) so you can await them:
JavaScript
async function* feed() {
try {
yield "a";
yield "b";
} finally {
console.log("cleanup");
}
}
const gen = feed();
// read: run to the next yield
const step = await gen.next(); // { value: "a", done: false }
// talk back: the paused yield evaluates to "reply"
const reply = await gen.next("reply"); // { value: "b", done: false }
// finish early: "cleanup" runs, then { value: "stop", done: true }
const end = await gen.return("stop");
// or signal a problem at the paused yield instead:
// await gen.throw(new Error("timeout"));
Browse
JavaScript AsyncGenerator Methods Table
All method guides in this JavaScript tutorial series (3 tutorials), grouped by what they do.
Resume and read values
1 method
Move the generator forward one step and receive the next value, optionally sending a value back in.
These language features work together with the three methods. They are part of the JavaScript syntax, not methods you call on the generator object.
Syntax
What it does
async function*
Declares an async generator function. Calling it returns an AsyncGenerator object without running the body.
for await...of
Loops over an async iterable, awaiting each next() result; calls return() when you leave the loop early.
yield
Pauses the generator and hands one value to the caller. A yielded promise is awaited first.
yield*
Delegates to another async or ordinary iterable and passes its values through one by one.
Symbol.asyncIterator
The method that makes an object async-iterable. An AsyncGenerator object returns itself from it.
Frequently Asked Questions
Three public methods: next() to resume and read the next value, return() to finish early, and throw() to raise an error at the paused yield. It also has a [Symbol.asyncIterator]() method that returns the object itself, which is what makes for await...of work.
The AsyncGenerator page explains the idea: async function*, for await...of, queueing, and cleanup, with beginner examples. This index lists the method guides so you can jump straight to next(), return() or throw().
Yes. next(), return() and throw() each return a Promise that resolves to { value, done }. The promise rejects when the generator throws an error it does not catch, for example after throw() with no matching catch block.
It calls next() for every step and return() when you leave the loop early with break, return, or an error. It never calls throw(); you call that one yourself.
Start with next(), because return() and throw() build on it. Then read return() for early exits and cleanup, and throw() for sending errors into a generator.