The neighbourhood head’s night patrol is a generator: what has run once g = ronda() is called, how far does the neighbourhood head walk each time Dina strikes…
Why it matters
Generators make lazy or endless sequences (IDs, streams, pagination), compact custom iterators, and are the base of async iterators for streaming. Understanding "pause and resume" opens patterns plain functions cannot easily copy.
The key idea
A generator stops at every yield and only goes on when pulled
Common mistakes
The page hangs after spreading an endless generator[...ronda()] keeps pulling until done: true, which never comes. Take a limited slice through .next(), or a loop that stops itself.
Thinking ronda() runs its body right away Calling it only makes a generator object. The code inside first runs on the first .next().
A finished generator used again gives no new values Once done: true, it is finished for good. Make a new object (ronda() again) to start over.
Try it in this playground
Make g = ronda() Nothing prints: the body has not run.
Pull g.next() The neighbourhood head walks to the next yield, then stops: done: false.
Pull until home The return value comes with done: true: { value: 'pulang', done: true }.
Pull once more after it is done { value: undefined, done: true }, forever.