What is generator functions
Watch the walkthrough
Open on InstagramRevision clarifications
Calling a generator function returns an iterator without executing the body. next resumes until yield or completion. An ordinary generator does not make CPU work asynchronous; async generators additionally work with asynchronous iteration.
The source notes below are copied unchanged apart from line endings. Examples are independent; some deliberately throw or depend on browser markup.
Original HackTheCode notes
Generator functions are a special type of function that can be paused and resumed, allowing you to manage asynchronous or iterative operations more effectively. They are defined using the function* syntax and use the yield keyword to pause the function execution and return a value.
function* numberGenerator() {
console.log("Before 1");
yield 1;
console.log("Before 2");
yield 2;
console.log("Before 3");
yield 3;
console.log("Before 4");
}
const ref = numberGenerator();
console.log(ref.next()); // Before 1 {value: 1, done: false}
console.log(ref.next()); // Before 2 {value: 2, done: false}
console.log(ref.next()); // Before 3 {value: 3, done: false}
console.log(ref.next()); // Before 4 {value: undefined, done: true}
Some advantages of Generators are
- Lazy Evaluation - This delays the evaluation of an expression until its value is needed.Memory
- Efficient - Due to Lazy Evaluation, generators are memory efficient.
An interesting use case for a javascript generator: generating an infinite number of unique identifiers!
function* generateId() {
let i = 0;
while (true) yield i++;
}
const getId = generateId();
console.log(getId.next().value); // 0
console.log(getId.next().value); // 1
console.log(getId.next().value); // 2