JS EXPERTChapter 9 · JS Expert
Generators
A generator function, written function*, can pause itself with yield and pick up again later. Calling it does not run the body; it returns a generator object. Each next() runs until the next yield and returns { value, done }. Generators produce values lazily, so they can even describe endless sequences, and they work directly with for...of and spread.
Worked example
How it reads
yieldhands out one value and pauses the function right there- Spreading
[...countdown(3)]pulls values until the generator finishes - The endless
idsgenerator is safe because values are only made on demand

Cloud tip: Never spread an endless generator. Take what you need with
next() or a loop with a break.

