Question presented to candidate: "If generator A does yield* generator B, and B has its own return statement, what happens to that return value — does it get yielded out to whoever's consuming A, or does something else happen to it?"
What a strong answer should cover:
- 📌 Interview term:
yield*— delegates iteration to another iterable (commonly another generator), forwarding every value it yields out to the outer generator's own consumer, one at a time, as if the outer generator had yielded each of them itself. - 📌 Interview term: the real, direct answer to the prompt — verified directly: the delegated generator's
returnvalue is genuinely NOT yielded out to the consumer — instead, the entireyield*expression itself evaluates to that return value, directly usable inside the OUTER generator's own code (e.g. assigned to a variable) — confirmed directly with a realconsole.logprinted from inside the outer generator immediately after theyield*line. - 📌 Interview term:
yield*works on any iterable, not just another generator — verified directly: delegating to a plain array withyield* [10, 20, 30]genuinely forwards each array element as its own yielded value, exactly like delegating to another generator. - 📌 Interview term: the manual equivalent, and what it's missing — verified directly: a hand-written
for (const v of innerGen()) yield v;loop produces the identical SEQUENCE of yielded values asyield*, but genuinely does not capture the delegated generator's return value the wayyield* innerGen()does as an expression. - A precise answer names that
yield*is the real, concise mechanism for COMPOSING generators — building a larger generator out of smaller ones without manually re-implementing the forwarding loop by hand each time.
Clarifying questions expected:
- None — this is a definitional/technical question; directly answering what happens to the delegated generator's return value is the strong signal, since it is the single most commonly missed detail about
yield*.
Code / implementation expected: Yes — a real nested generator setup where the OUTER generator captures and logs the INNER generator's return value is the clearest, most convincing demonstration.