asyncGenerator.next() resumes an async generator until its next yield (or its end) and returns a Promise of a result object { value, done }: value is what was yielded, and done says whether the generator has finished.
Pass an argument, next(value), to send data into the generator: the paused yield expression evaluates to that value. for await...of calls next() for you; call it yourself when you want one value at a time or need to talk back.
Remember
const result = await gen.next();
result.value → the yielded value
result.done → false while values keep coming
true once the generator has finished
next(x) → x becomes the result of the paused yield
first next(x) → x is ignored (no yield is waiting yet)
after the end → { value: undefined, done: true }
Syntax
Call an async function* to get the generator object, then call next() on it. Always await the result (or use .then()), because it is a Promise.
JavaScript
const gen = myAsyncGenerator();
const first = await gen.next();
const reply = await gen.next(value);
Call
What it does
asyncGenerator.next()
Run to the next yield; get a Promise of { value, done }.
asyncGenerator.next(value)
Same, and the paused yield evaluates to value.
asyncGenerator.next().then(fn)
Without await: fn receives { value, done }.
Built into JavaScript (ES2018); no import required. Siblings: return() and throw().
Create, do not run. Calling countdown(3) only builds the generator object; the loop body has not started yet.
One next(), one value. Each await ticker.next() runs the body up to the next yield and resolves to { value: n, done: false }.
Past the last yield. The fourth call lets the loop end, so done flips to true; the fifth call shows that a finished generator keeps answering value: undefined, done: true.
Example 2 — Send Values Back with next(value)
A running total for a shopping cart: every next(amount) sends a price in and gets the new total back.
JavaScript
async function* runningTotal() {
let total = 0;
while (true) {
const amount = yield total;
await Promise.resolve(); // pretend to save the cart
total += amount;
}
}
async function run() {
const cart = runningTotal();
console.log("start: " + (await cart.next("ignored")).value);
console.log("+25 -> " + (await cart.next(25)).value);
console.log("+10 -> " + (await cart.next(10)).value);
console.log("+5 -> " + (await cart.next(5)).value);
}
run();
Await inside the body.checkStatus(i) returns a promise, and the generator waits for it before deciding whether to yield or stop.
Loop on done. The while (!result.done) loop keeps calling next() and prints each status while the job is still busy.
Return a summary. On "finished", the return statement ends the generator; that last result has done: true and the summary text as its value.
Compatibility
Browser & Runtime Support
AsyncGenerator.prototype.next() arrived with async iteration in ES2018, together with async function* and for await...of.
✓ Baseline · ES2018
AsyncGenerator.prototype.next()
Supported in Chrome 63+, Firefox 55+, Safari 12+, Edge 79+, and Node.js 10+. Not available in Internet Explorer.
96%Modern browser support
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
AsyncGenerator next()Excellent
Bottom line: Safe for every current browser and Node.js LTS release; very old engines need a transpiler. The examples print plain strings only, so the output is the same in Node.js and in the browser.
Frequently Asked Questions
A Promise that resolves to { value, done }: value is what the generator yielded or returned, and done is false while values keep coming and true once it has finished. Use await or .then() to read it.
An argument becomes the result of the yield where the generator is paused. Before the first call nothing is paused yet, so the value is dropped. Send your first real value with the second call.
The calls are queued and handled one after another, in the order you made them; each promise resolves with its own { value, done }. So Promise.all([gen.next(), gen.next()]) gives the first and second values in order.
Use for await...of when you want every value: it calls next() for you, stops at done: true, and calls return() if you break early. Call next() yourself to take one value at a time, to send values back, or to read the final return value, which the loop ignores.
A promise of { value: undefined, done: true }, every time. It never restarts the generator and never throws; create a new generator object to start again.