Skip to solution
hardFrontend

What does yield* do in a generator?

1.0k views
01

Understand 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
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript fundamentals interviews. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. The exact real return-value capture below was actually run in Node.

1. Why This Even

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

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

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 127 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track