yield pauses the generator and can be called multiple times, while return ends the generator and sets done: true; further calls to next() return { done: true }.
yield is used to produce intermediate values without terminating the generator; you can have many yield statements. return terminates the generator; the returned value is the final value and done becomes true. If no return is present, the generator implicitly returns undefined. After a return, the generator cannot be resumed.
Write a simple generator that yields numbers 1 to 3 and then returns the string 'done'. What will a for...of loop output when iterating over it?
If you call next() on a generator after it has executed a return statement, what value do you get back and why?
You have a function that streams lines from a large file using a generator. A teammate changed a yield to return to signal end-of-file, and the consumer now only gets the first line. Explain why this happened and how to fix it.
During debugging, you notice that a generator used in an async pipeline never releases a file handle. The code uses return inside a try...finally. How does return affect the generator’s cleanup compared to yield?
Design a data‑processing component that can pause, resume, and early‑exit based on back‑pressure, using generators. Discuss how you would use yield for incremental results and return for signalling completion or error, and the trade‑offs for memory usage.
In a high‑throughput server, you replace a hand‑rolled iterator with a generator to lazily fetch DB rows. How would you ensure that early termination via return correctly closes the DB cursor, and what pitfalls could arise if you mistakenly used yield for termination?
Our platform is migrating legacy callback‑based streaming APIs to generator‑based pipelines across multiple services. As the lead architect, outline a strategy for handling the semantic shift from return‑based termination to yield‑based flow, considering backward compatibility, error propagation, and observability.
When designing a cross‑team library for composable data streams, you need to define a convention for when a generator should return a final value versus when it should yield a sentinel. What factors (performance, API ergonomics, debugging) influence this decision, and how would you document and enforce it?