Generator Functions
A generator function creates a generator object. Its body starts on next(), pauses at yield, and resumes on a later next() call.
Creating the generator prepares an execution; it does not run the whole function immediately.
See the mechanism
generator objectbody not startedFollow one example
The first next starts the body and yields Open. The second resumes and yields Read. The third reaches return and reports Finished with done true. for...of includes yielded values but does not include that final return value. Generators are controlled execution, not automatic background threads.
Predict, change and check
Predict: Will [...readingSteps()] contain Finished as its third item?
Test your explanation: Compare spread with the three explicit next results. Add a log before the first yield to observe that the body starts only when next is called.
The tasks below ask you to explain that result and apply the idea to different inputs.
Apply and distinguish
A generator call creates an iterator
Calling a generator function does not immediately run its whole body. It returns a generator object that starts or resumes when next is called. yield exposes one value and suspends execution; an ordinary return completes the generator rather than adding a normal for...of item.
A generator object is normally a one-shot traversal. Call the generator function again for a fresh run. Use a short finite sequence while learning the protocol, and inspect done separately from value. An infinite generator is safe only with a consumer that has a deliberate stopping policy.
Check your understanding: State the input contract, identify the operation that changes the result, and give one boundary input that would distinguish the correct rule from the mistake described above.