Generator
A generator is a language construct or computation that produces values incrementally on demand, typically suspending and resuming while preserving its state.
Secrets of the JavaScript Ninja, Second Edition
A JavaScript function that can yield values, suspend execution, and later resume with its local state preserved.
- How it is used
- Uses generators to produce values on demand, simplify traversal, and express dependent asynchronous tasks without blocking the UI.
- How deep it goes
- Explains syntax, iterator control, two-way communication, state transitions, and retained execution contexts through worked examples.
- Examples
- Demonstrates yielded strings, a local ID counter, recursive DOM traversal, and dependent JSON requests.
- What you can do after
- Build it from scratch with function*, yield, next(), and yield* to generate sequences and traverse a DOM tree.
Read how Secrets of the JavaScript Ninja, Second Edition explains generator →
Python Workout: 50 ten-minute exercises
A generator function is a Python function that produces a generator by yielding successive values while preserving its execution state between yields.
- How it is used
- Deploys generator functions to implement iterators, stream data, and avoid materializing large result collections.
- How deep it goes
- Explains yield, automatic completion, suspended execution, and retained locals, but defers generator expressions to a separate treatment.
- Examples
- Demonstrates small yielded sequences, directory-wide line streaming, elapsed-time reporting, chaining iterables, and memory-saving sums.
- What you can do after
- Build it from scratch as a stateful iterator that yields, filters, transforms, or augments values incrementally.
Read how Python Workout: 50 ten-minute exercises explains generator →
Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond
An iterator-like object returned by a generator function that controls resuming the generator and retrieving yielded values.
- How it is used
- Produces values on request and lets outside code resume execution or pass information back.
- How deep it goes
- Works through creation, pauses, two-way yields, and completion, then applies them to sequences without pursuing broader generator internals.
- Examples
- Uses infinite number and Fibonacci sequences, a feedback-driven simulation, and a timed game turn.
- What you can do after
- Build it from scratch: create generators that yield sequences, accept feedback through next(), or pause work between timed calls.
Python Workout, Second Edition
A generator function is a Python function containing yield that returns a generator object when called and resumes execution incrementally when iterated.
- How it is used
- Implements function-shaped iterators for incremental, filtered, transformed, or augmented data processing.
- How deep it goes
- Explains yield suspension, automatic termination, retained local state, and practical file and timing generators.
- Examples
- Uses line streaming across files, elapsed-time reporting, chaining iterables, filtering, and reimplementations of zip and range.
- What you can do after
- Build it from scratch as a lazy iterator that filters, modifies, combines, or supplements values from an iterable.
Read how Python Workout, Second Edition explains generator →
The Joy of JavaScript
A generator is a function or function-derived object in JavaScript that can produce a sequence of values over time and support iterator semantics.
- How it is used
- Uses generators to create iterables, model streams, process asynchronous chunks, and connect pull-based iteration with observables.
- How deep it goes
- Explains syntax, iterator results, yielding, async iteration, and stream integration, but does not develop generator runtime internals beyond suspended context.
- Examples
- Demonstrates yielded words, generated blockchain blocks, iterable validation branches, file-backed block streams, and observable pipelines.
- What you can do after
- Build generators and async generators, consume them with iteration syntax, and connect them to streamed data processing.
Python How-To
A generator is a Python construct that produces a sequence of values incrementally through iteration rather than computing the entire sequence at once.
- How it is used
- Uses generators as memory-efficient data providers for sequence calculations and file processing.
- How deep it goes
- Explains yield, resumption, exhaustion, generator expressions, and measured object sizes; mentions generator-based coroutines without teaching them.
- Examples
- Demonstrates sums of perfect squares and matching data files, and challenges readers to generate Fibonacci numbers.
- What you can do after
- Build generators from scratch using yield or generator expressions, and consume or recreate them during iteration.
Learn Haskell by Example
A generator is a list-comprehension clause that supplies values from a list or other sequence for subsequent comprehension clauses.
- How it is used
- Builds lists by supplying source values to comprehensions that transform and optionally filter them.
- How deep it goes
- Explains generator evaluation, guards, Cartesian products, and pattern matching without descending into implementation internals.
- Examples
- Uses numeric ranges, Cartesian products, word transformations, and Maybe-pattern filtering as concrete examples.
- What you can do after
- Builds filtered and combined lists with generators, guards, multiple sources, and matching patterns.
Secrets of the JavaScript Ninja, Third Edition
A generator is a JavaScript function whose execution can suspend and resume while producing a sequence of values through an iterator.
- How it is used
- Provides interruptible iteration as an alternative to synchronous or asynchronous loops, while serving mostly niche scenarios today.
- How deep it goes
- Explains generator syntax, next(), yield suspension, iterator results, two-way values, delegation, and async-iterator use.
- Examples
- Demonstrates infinite random numbers, a two-way multiplier, delegated names, and yielding fetch promises.
- What you can do after
- Apply generators to produce iterable sequences, suspend and resume execution, exchange values through next(), and delegate with yield*.
Read how Secrets of the JavaScript Ninja, Third Edition explains generator →
Functional Programming in JavaScript
A JavaScript function form that can pause and resume execution, yielding a sequence of values over time.
- How it is used
- Produces large, infinite, or asynchronously available data sequences incrementally through lazy iteration.
- How deep it goes
- Explains function* syntax, yield, saved execution context, iterators, composition, and traversal, without detailing iterator internals.
- Examples
- Uses range generation, bounded consumption, squared values, nested student generators, and recursive tree traversal.
- What you can do after
- Apply it via JavaScript generators to create, transform, consume, and compose lazy sequences.
Read how Functional Programming in JavaScript explains generator →
Fast Python: High performance techniques for large datasets
- How it is used
- Uses lazy generation to process large sequences with less memory and to avoid computing results that are never needed.
- How deep it goes
- Explains on-demand execution, yield-based iteration, and memory trade-offs, but does not detail generator internals.
- Examples
- Uses a CSV temperature reader and pandas chunks to demonstrate incremental processing.
- What you can do after
- Apply generators to process CSV values or data-frame chunks without first loading the complete result into memory.
Read how Fast Python: High performance techniques for large datasets explains generator →
Python Concurrency with asyncio
A generator is a Python iterator object that produces values lazily by suspending and resuming execution around yield points.
- How it is used
- Uses generators for memory-efficient lazy sequences and asynchronous production of values such as database rows.
- How deep it goes
- Explains creation, suspension, resumption, next, for, async for, and generator-based coroutines through runnable examples.
- Examples
- Demonstrates integer generators, delayed asynchronous integers, generator-based coroutines, and a custom future advanced by iteration.
- What you can do after
- Apply it via Python's next, for, async for, yield, and yield from constructs to produce values incrementally.
Read how Python Concurrency with asyncio explains generator →
Data Pipelines with Apache Airflow, Second Edition
A generator is a Python iterable that produces values incrementally rather than materializing them all at once.
- How it is used
- Uses lazy iteration to hide paginated API retrieval and support efficient existence checks in Airflow sensors.
- How deep it goes
- Explains generator creation with yield from, deferred page fetching, and StopIteration handling without covering generator internals.
- Examples
- Demonstrates paginated MovieLens ratings retrieval and a sensor that tests for one available record.
- What you can do after
- Apply it via Python generators to stream paginated API records or test whether a result exists without fetching everything.
Read how Data Pipelines with Apache Airflow, Second Edition explains generator →
Practices of the Python Pro
A generator is a Python construct that produces a sequence of values incrementally while preserving its execution state between requests.
- How it is used
- Reduces memory usage when iterating over values that do not need to exist simultaneously.
- How deep it goes
- Defines incremental production, explains yield-based pausing, and demonstrates a range-like implementation.
- Examples
- Uses range-like counting, squared-number generation, and chained generators to illustrate incremental computation and memory use.
- What you can do after
- Build it from scratch with yield-based loops and choose it over lists when all results are not needed at once.
Also covered in
- Evolutionary Deep Learning: Genetic algorithms and neural networks
- Digital Twins in Action
- The Quick Python Book, Fourth Edition
- Math for Web Design
- Learn C++ by Example
- Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu
- Designing AI SystemsIn early access (MEAP)
- The Coder CafeIn early access (MEAP)
- Knowledge Graphs and LLMs in Action
- Essential TypeScript 5, Third Edition
- Software Design for Python Programmers
- F# in Action: A Revised Edition of Get Programming with F#
What to know first
- functionA function is a callable unit of program code that performs a computation or operation, can accept inputs, and may produce a result or side effect.
- PythonPython is a high-level, general-purpose, dynamically typed programming language with readable syntax and support for multiple programming paradigms.
- iteratorAn iterator is an object or abstraction that provides successive values from a collection or other data source through a traversal interface.
- loopA loop is a programming-language control-flow construct that repeatedly executes a block of instructions according to an iteration rule or continuation condition.
Related concepts
- yielduses · Yield is a language construct that transfers control or a value from a computation to another context, with behavior that varies by language.
- functionkind of · A function is a callable unit of program code that performs a computation or operation, can accept inputs, and may produce a result or side effect.
- iteratorkind of · An iterator is an object or abstraction that provides successive values from a collection or other data source through a traversal interface.
- nextused by · An iterator operation that advances an iterator and returns its next value or result, with behavior on exhaustion determined by the API.
- coroutineuses · A coroutine is a computation that can suspend and later resume execution while retaining its state, enabling cooperative transfer of control.
Questions about generator
What is generator?
A generator is a language construct or computation that produces values incrementally on demand, typically suspending and resuming while preserving its state.
Which Manning books explain generator?
Secrets of the JavaScript Ninja, Second Edition, Python Workout: 50 ten-minute exercises, Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond, Python Workout, Second Edition, The Joy of JavaScript, Python How-To, Learn Haskell by Example, Secrets of the JavaScript Ninja, Third Edition and 5 more.
What should I know before learning generator?
Manning books that teach generator usually introduce function, Python, iterator and loop first.
What is generator a kind of?
Generator is a kind of function. A function is a callable unit of program code that performs a computation or operation, can accept inputs, and may produce a result or side effect.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.












