Skip to the solution
HardFrontend6 min read

What does yield* do in a generator?

1.0k views

The problem

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 return value is genuinely NOT yielded out to the consumer β€” instead, the entire yield* 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 real console.log printed from inside the outer generator immediately after the yield* line.
  • πŸ“Œ Interview term: yield* works on any iterable, not just another generator β€” verified directly: delegating to a plain array with yield* [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 as yield*, but genuinely does not capture the delegated generator's return value the way yield* 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.

  • generators

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof: yield* forwards yielded values to the consumer while the delegated generator's return value is captured as the yield* expression's own result

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 127 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextHard

What are async iterators and for await...of?

Continue