A generator is a function that can pause. next() is the button that makes it run. Each call starts or resumes the generator and lets it run until the next yield, where it pauses again.
The method always returns a small object with two properties: value (what was yielded) and done (false while there is more to come, true when the generator has finished). You can also give next() a value, and the paused yield receives it.
Remember
const result = generator.next(valueToSend);
result.value → what the generator yielded (or returned)
result.done → false = paused, more to come
true = finished
valueToSend becomes the answer of the paused yield.
The very first next() ignores it.
Syntax
Create a generator object by calling a function* function, then call next() on that object.
Optional. A value that is sent into the generator. It becomes the result of the yield expression where the generator is paused. The first call to next() has no paused yield yet, so the value is ignored.
Return Value
Return Value
Description
{ value, done: false }
The generator paused at a yield. value is what was yielded.
{ value, done: true }
The generator finished. value is what the generator returned, or undefined when it returned nothing.
TypeError (thrown)
next() was called while the generator was already running, for example from inside the generator itself.
JavaScript Examples
Example 1 — Run a Generator One Step at a Time
The generator prints a line before each yield. Watch when each line appears.
Creating is not running.steps() only builds the generator object, so the first line printed is the one outside the generator.
Each next() runs to a yield. The first next() prints "inside: start" and pauses at yield "first". The text "inside: ..." appears before the matching result line, because the generator runs while next() is being evaluated.
The third call finishes it. The generator prints "inside: end" and runs off the end, so done is true and value is undefined. The fourth call is harmless and gives the same result.
Example 2 — Send a Value into a Generator
The value you give to next() becomes the answer of the yield that was waiting.
JavaScript
function* chat() {
const name = yield "What is your name?";
const hobby = yield "Hello " + name + "! What do you like?";
return name + " likes " + hobby;
}
const gen = chat();
console.log(gen.next("ignored").value);
console.log(gen.next("Ava").value);
const last = gen.next("chess");
console.log(last.value);
console.log(last.done);
What is your name?
Hello Ava! What do you like?
Ava likes chess
true
How It Works
The first next() ignores its value. No yield is waiting yet, so "ignored" goes nowhere. The generator runs to the first yield and gives back the question.
The second next() answers it.gen.next("Ava") makes the waiting yield produce "Ava", so name is "Ava". The generator then asks the second question.
The last next() finishes.hobby becomes "chess" and the return sends back the final sentence. Because the generator ended, done is true.
Example 3 — Pull Values from an Endless Generator
This generator never finishes, but it does not need to, because it only works when asked.
JavaScript
function* ids() {
let id = 1;
while (true) {
yield "user-" + id;
id++;
}
}
const gen = ids();
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
const firstFour = [];
for (const id of ids()) {
firstFour.push(id);
if (firstFour.length === 4) {
break;
}
}
console.log(firstFour.join(", "));
A loop that does not hang.while (true) would freeze a normal function. In a generator it pauses at every yield, so the loop only moves one step per next().
Ask for what you need. Three calls to gen.next().value give user-1, user-2 and user-3. The generator remembers id between calls.
for...of calls next() for you. The loop stops itself with break after four values. It uses a fresh ids() generator, so it starts again at user-1.
Compatibility
Browser & Runtime Support
Generators arrived with ES2015. Every current browser and Node.js version supports them, including next().
✓ ES2015 · Widely supported
generator.next()
Supported in Chrome 39+, Edge 13+, Firefox 26+, Safari 10+ and current Node.js, Deno and Bun. Internet Explorer never got generators.
ES2015Standard since
Google Chrome39+ · Desktop & Mobile
Full support
Microsoft Edge13+ · Legacy & Chromium
Full support
Mozilla Firefox26+ · Desktop & Mobile
Full support
Apple Safari10+ · macOS & iOS
Full support
Opera26+ · Chromium based
Full support
Samsung InternetRecent versions · Android
Supported
Android WebViewRecent versions · Chromium based
Supported
Node.jsAll current versions · Server runtime
Supported
DenoAll versions · JavaScript runtime
Supported
BunAll versions · JavaScript runtime
Supported
Internet ExplorerNo native support
Not supported
generator.next()Excellent
Bottom line: Safe to use in every current browser and in Node.js. Only Internet Explorer lacks function*, and it is no longer supported.
Frequently Asked Questions
It starts or resumes the generator and runs it until the next yield (or until it finishes). It returns an object with two properties: value, the yielded value, and done, which is false while the generator can give more values and true when it is finished.
A value passed to next() becomes the result of the yield where the generator is paused. Before the first next() there is no paused yield yet, so the value has nowhere to go and is ignored. Pass values from the second call on.
Nothing bad. It keeps returning { value: undefined, done: true } and the generator code does not run again. That makes it safe to keep calling next() in a loop until done is true.
No. for...of, the spread operator [...gen] and Array.from(gen) call next() for you until done is true. Call next() by hand when you want full control, for example to send values in or to pull only a few values.
yield pauses the generator and gives a value out with done: false, and the generator can continue later. return finishes the generator and gives a final value with done: true. Loops such as for...of ignore the returned value and only use the yielded ones.