JS Generators
In this page:
function* generatorName() {
yield value1;
yield value2;
}
const generator = generatorName();
generator.next();
What Is a Generator?
A generator is a special function that can pause and continue later. It is declared with an asterisk. Unlike a normal function that runs to completion, a generator's execution can be suspended mid-function and resumed exactly where it left off.
उदाहरण: What Is a Generator?
// Define the generator function `numberGenerator` with no parameters
// Define the generator function `numberGenerator` with no parameters
function* numberGenerator() {
yield 1;
yield 2;
}
// Declare the constant `gen`, set to `numberGenerator()`
// Declare the constant `gen`, set to `numberGenerator()`
const gen = numberGenerator();
// Print `gen.next()` to the console
// Print `gen.next()` to the console
console.log(gen.next());
yield
yield pauses a generator and sends a value to the caller. The next() method continues from that point.
Calling gen.next() runs the generator up to the next yield (or to the end), returning { value, done } — this makes generators a controllable, step-by-step source of values.
उदाहरण: yield
function* counter() {
yield 1;
yield 2;
}
const gen = counter();
console.log(gen.next()); // { value: 1, done: false }
console.log(gen.next()); // { value: 2, done: false }
console.log(gen.next()); // { value: undefined, done: true }
Generator Iteration
Generators are iterable. A for...of loop can read their yielded values. This means you can iterate a generator's output the same way you'd iterate an array, without first collecting every value into memory.
उदाहरण: Generator Iteration
// Define the generator function `colors` with no parameters
// Define the generator function `colors` with no parameters
function* colors() {
yield "red";
yield "green";
}
// Loop over `colors()`, binding each item to `color`
// Loop over `colors()`, binding each item to `color`
for (const color of colors()) {
// Print `color` to the console
// Print `color` to the console
console.log(color);
}
Sending Values
next() can send a value back into a paused generator. This lets generators communicate with the caller.
You can also pass a value into next(value), which becomes the result of the paused yield expression inside the generator, enabling two-way communication.
उदाहरण: Sending Values
// Define the generator function `echo` with no parameters
// Define the generator function `echo` with no parameters
function* echo() {
// Declare the constant `received`, set to `yield "ready"`
// Declare the constant `received`, set to `yield "ready"`
const received = yield "ready";
// Print `"Got:", received` to the console
// Print `"Got:", received` to the console
console.log("Got:", received);
}
// Declare the constant `gen`, set to `echo()`
// Declare the constant `gen`, set to `echo()`
const gen = echo();
// Print `gen.next()` to the console
// Print `gen.next()` to the console
console.log(gen.next());
// Call `gen.next("hello")`
// Call `gen.next("hello")`
gen.next("hello");
Practical Use
Generators are useful for controlled sequences, custom iterables, and lazy data processing.
Because a generator computes each value only when asked, it's a natural fit for infinite sequences or expensive computations you only want to run as far as actually needed.
उदाहरण: Practical Use
// Define the generator function `infiniteCount` with no parameters
// Define the generator function `infiniteCount` with no parameters
function* infiniteCount() {
// Declare the variable `n`, set to `1`
// Declare the variable `n`, set to `1`
let n = 1;
while (true) yield n++;
}
// Declare the constant `gen`, set to `infiniteCount()`
// Declare the constant `gen`, set to `infiniteCount()`
const gen = infiniteCount();
// Print `gen.next().value, gen.next().value, gen.next().value` to the console
// Print `gen.next().value, gen.next().value, gen.next().value` to the console
console.log(gen.next().value, gen.next().value, gen.next().value);
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: