Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Generator.prototype.next() — resuming a generator

The `next()` method starts or resumes a synchronous generator and returns an iterator result object. Its optional argument becomes the result of a suspended plain `yield` expression; the first call's argument is ignored.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

generator.next()
generator.next(value)

Generator.prototype.next is the method used as generator.next(). Calling a function* function creates a generator without executing its body. The first next() starts execution; later calls resume it until it suspends again or completes.

For a plain yield, the result contains the yielded value in value and has done: false. When the generator completes normally, the result has done: true and value is the return value, or undefined if no value was provided. Further calls after completion return { value: undefined, done: true }.

During yield* delegation, if done on the result from the delegated iterator's next() is falsy, the outer generator's next() returns that same object unchanged. For example, if the delegated iterator returns {}, the missing done is treated as false, and the returned object still has neither value nor done.

When the generator is suspended at a plain yield, the argument to next(value) becomes the result of that expression. In const input = yield "ready", the first step exposes "ready", and the subsequent next(7) assigns 7 to input. The first call's argument is ignored because the generator is not yet suspended at a yield.

Re-entering an executing generator through next() throws TypeError. Each instance traverses its sequence only once; call the generator function again to start over.

Examples

function* exchange() {
  const input = yield "ready";
  return input * 2;
}

const generator = exchange();

function print(result) {
  console.log(String(result.value) + " | " + result.done);
}

print(generator.next(999));
print(generator.next(7));
print(generator.next());

Result

ready | false
14 | true
undefined | true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources