Skip to content

generator

A generator is an object that runs a function body in steps, pausing at each value it produces and resuming only when code asks for the next one.

Generators come from generator functions, which are declared with function* and hand out each value with yield. Calling one doesn’t run its body. The call returns a generator, which is both an iterator and an iterable and inherits from Iterator.

Each call to the generator’s next() method runs the body until the following yield, then freezes it with its local variables intact. Because values are computed only on request, a generator can describe a sequence with no end. Generators are a common way to write custom iterators, and for...of and spread syntax consume them directly.

Example

Say a report splits a list of orders into pages, and the code that slices each page should run only when a page is needed:

Language: JavaScript
> function* paginate(orders, pageSize) {
…   for (let start = 0; start < orders.length; start += pageSize) {
…     console.log(`Slicing from ${start}`);
…     yield orders.slice(start, start + pageSize);
…   }
… }
undefined
> const pages = paginate(["A1", "A2", "A3", "A4", "A5"], 2);
undefined

> pages.next()
Slicing from 0
{ value: [ 'A1', 'A2' ], done: false }

> [...pages]
Slicing from 2
Slicing from 4
[ [ 'A3', 'A4' ], [ 'A5' ] ]
> pages.next()
{ value: undefined, done: true }

Calling paginate() prints nothing, because the body hasn’t started. The first next() runs it up to the first yield and returns the page in an object whose done property says whether the generator has finished. The spread resumes from that point and drains the rest. After that, the generator is finished and can’t be restarted.

The following stepper runs the same calls in any order and shows where the body is paused, what each call prints, and what it returns:

Interactive diagram — enable JavaScript to view.

Leaving a for...of loop early also finishes a generator. Say a log reader stops at the first error:

Language: JavaScript
> function* readLog() {
…   try {
…     yield "INFO start";
…     yield "ERROR disk full";
…     yield "INFO retry";
…   } finally {
…     console.log("Closing log");
…   }
… }
undefined
> const lines = readLog();
undefined

> for (const line of lines) {
…   if (line.startsWith("ERROR")) break;
… }
Closing log
undefined
> lines.next()
{ value: undefined, done: true }

The break makes the loop call the generator’s return() method, which runs the finally block and closes the generator. The third line is never produced, even with a later next() call.

A developer pulls a lever on a machine whose glass chamber releases glowing capsules one at a time onto a conveyor, with panels labeled Iterator, Next, Yield, Done, and Await.

Course

JavaScript Iterators and Generators for Pythonistas

Port a weather station's sensor feed from Python to JavaScript, and learn to write iterators and function* generators, chain lazy iterator helpers, and read async feeds with for await...of.

advanced js py

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 19, 2026