Generators in JavaScript
Generators are special functions that can pause execution and resume later, allowing you to produce a sequence of values over time rather than computing them all at once.Basic Generator Syntax
Generators are defined using thefunction* syntax and use the yield keyword to pause execution.
Using Generators with for…of
Generators are iterable, so they work withfor...of loops:
Infinite Sequences
Generators can produce infinite sequences because values are computed lazily:Fibonacci Generator
Passing Values to Generators
You can pass values to a generator usingnext(value):
The first
next() call starts the generator but doesn’t pass a value. Subsequent next(value) calls pass the value to the last yield.yield* (Delegating to Another Generator)
You can delegate to another generator usingyield*:
Practical Examples
ID Generator
Range Generator
Batch Processing
Tree Traversal
Async Data Fetching
Generator Methods
return()
Force a generator to complete:throw()
Throw an error into the generator:Lazy Evaluation
Generators enable lazy evaluation - values are computed only when needed:Async Generators
Async generators combine generators with async/await:Async Data Stream
Use Cases
Infinite Sequences
Infinite Sequences
Generate infinite sequences like IDs, random numbers, or mathematical sequences without consuming memory.
Lazy Evaluation
Lazy Evaluation
Process large datasets or streams one item at a time without loading everything into memory.
Iteration Control
Iteration Control
Create custom iterators for complex data structures like trees, graphs, or custom collections.
State Machines
State Machines
Implement state machines where each
yield represents a state transition.Async Operations
Async Operations
Coordinate multiple async operations with async generators, useful for pagination or streaming.
Best Practices
Use generators for large sequences
Use generators for large sequences
When working with large or infinite sequences, generators save memory by computing values on demand.
Combine with iterators
Combine with iterators
Generators are perfect for creating custom iterators for complex data structures.
Leverage lazy evaluation
Leverage lazy evaluation
Use generator pipelines to process data lazily, computing only what’s needed.
Handle errors properly
Handle errors properly
Use try/catch blocks inside generators to handle errors during iteration.