JavaScript AsyncGenerator next() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
AsyncGenerator

Definition

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);
CallWhat 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().

Library declarations
JavaScript
// AsyncGenerator.prototype.next (ES2018)
asyncGenerator.next()
asyncGenerator.next(value)

// returns Promise<{ value: any, done: boolean }>

Parameters

ParameterDescription
valueOptional. The paused yield expression evaluates to it. Ignored on the first call, when no yield is waiting yet.

Return Value

Return ValueDescription
Promise of { value, done: false }A yield was reached; value is the yielded value (a yielded promise is awaited first).
Promise of { value, done: true }The generator finished; value is the return value or undefined.
Rejected promiseThe body threw an error it did not catch. The generator is finished afterwards.

JavaScript Examples

Example 1 — Read a Countdown One Step at a Time

Call next() by hand and print every result object, including the ones after the end.

JavaScript
async function* countdown(from) {
  for (let n = from; n > 0; n--) {
    await Promise.resolve(); // pretend to wait for a timer
    yield n;
  }
}

function show(result) {
  return "value: " + result.value + ", done: " + result.done;
}

async function run() {
  const ticker = countdown(3);
  console.log(show(await ticker.next()));
  console.log(show(await ticker.next()));
  console.log(show(await ticker.next()));
  console.log(show(await ticker.next()));
  console.log(show(await ticker.next()));
}

run();
Try It Yourself

How It Works

  1. Create, do not run. Calling countdown(3) only builds the generator object; the loop body has not started yet.
  2. One next(), one value. Each await ticker.next() runs the body up to the next yield and resolves to { value: n, done: false }.
  3. 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();
Try It Yourself

How It Works

  1. Prime the generator. The first next("ignored") runs up to yield total and reports 0; its argument is dropped because no yield was waiting.
  2. Send an amount in. next(25) resumes the paused yield, so amount becomes 25 before the total is updated.
  3. Two-way traffic. Every later call delivers a new amount and receives the new total in the same step: 35, then 40.

Example 3 — Poll a Job Until It Finishes

Drive the generator with a while loop and stop when done is true; the final result carries a summary.

JavaScript
const statuses = ["queued", "running", "running", "finished"];

function checkStatus(i) {
  return new Promise((resolve) => {
    setTimeout(() => resolve(statuses[i]), 5);
  });
}

async function* pollJob() {
  for (let i = 0; i < statuses.length; i++) {
    const status = await checkStatus(i);
    if (status === "finished") {
      return "job finished after " + (i + 1) + " checks";
    }
    yield status;
  }
}

async function run() {
  const poller = pollJob();
  let result = await poller.next();
  while (!result.done) {
    console.log("status: " + result.value);
    result = await poller.next();
  }
  console.log(result.value);
}

run();
Try It Yourself

How It Works

  1. Await inside the body. checkStatus(i) returns a promise, and the generator waits for it before deciding whether to yield or stop.
  2. Loop on done. The while (!result.done) loop keeps calling next() and prints each status while the job is still busy.
  3. Return a summary. On "finished", the return statement ends the generator; that last result has done: true and the summary text as its value.

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 Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · 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.

Next: return()

Learn how to finish an async generator early and make sure its cleanup code runs.

return() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful