JavaScript generators are a special type of function that can pause and resume their execution. They provide a way to handle asynchronous operations, manage state, and work with iterators more effectively.
Here’s an in-depth look at what generators are, how they work, and how to use them.
1. Defining Generators
Generators are defined using the function* syntax and can yield multiple values over time. Here’s an example:
When you invoke a generator function, it doesn't execute immediately. Instead, it returns an iterator, which you can use to control the generator's execution:
The yield the keyword is used to pause the execution of the generator function and return a value. The generator can be resumed later from where it left off:
Generators are useful for creating custom iterators. Here’s an example of an iterator that generates an infinite sequence of numbers:
function* infiniteSequence() {
let i = 0;
while (true) { // infinite loop test
yield i++; // increase value of 'i' and return them
}
}
const sequence = infiniteSequence();
console.log(sequence.next().value); // 0
console.log(sequence.next().value); // 1
console.log(sequence.next().value); // 2
// and so on...
for(item of sequence){
//console.log(item); // for infinite interation
}
Note
Generators are functions that can pause and resume execution.
function*: Define a generator.
yield: Pause the generator and return a value.
.next(): Resume the generator from where it left off.
Custom Iterators: Use generators to create custom iterators.
Control Flow: Pass values and handle errors within generators.
Async Operations: Combine generators with Promises to manage asynchronous code.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
JavaScript generators are a special type of function that can pause and resume their execution. They provide a way to handle asynchronous operations, manage state, and work with iterators more effectively.
Here’s an in-depth look at what generators are, how they work, and how to use them.
1. Defining Generators
Generators are defined using the
function*syntax and can yield multiple values over time. Here’s an example:2. Invoking Generators
When you invoke a generator function, it doesn't execute immediately. Instead, it returns an iterator, which you can use to control the generator's execution:
3. The
yieldKeywordThe
yieldthe keyword is used to pause the execution of the generator function and return a value. The generator can be resumed later from where it left off:4. Using Generators for Iteration
Generators are useful for creating custom iterators. Here’s an example of an iterator that generates an infinite sequence of numbers:
Note
function*: Define a generator.yield: Pause the generator and return a value..next(): Resume the generator from where it left off.