Skip to content
Latchkey

What Is a Generator?

A generator is a special function that yields values one at a time, suspending its execution after each yield and resuming where it left off when the next value is requested. It keeps its local state across these pauses without building the whole result in memory. Generators are a convenient way to express lazy, on-demand sequences.

Why it matters

Producing values lazily lets a generator handle large or even infinite sequences with constant memory. It cleanly separates how items are produced from how a consumer iterates over them.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →