Skip to solution
hardFrontend

What is a generator function?

806 views
01

Understand the problem

Question presented to candidate: "A generator function looks like it 'returns' multiple times via yield. What actually happens when you call one — does its body run immediately, and can you pass a value BACK INTO it while it's paused?"

What a strong answer should cover:

  • 📌 Interview term: function* — declares a generator function; calling it does not run its body — it returns a genuine generator object (both an iterator and an iterable, covered in this bank's own dedicated iterator/iterable question) that controls execution via .next().
  • 📌 Interview term: the real, direct answer to the prompt's laziness question — verified directly: calling a generator function genuinely does not execute any of its body — a console.log as the very first line inside the body genuinely did not print until the FIRST .next() call, confirmed by the exact real ordering of output.
  • 📌 Interview term: pause and resume — each yield genuinely pauses execution, returning { value, done: false }; the next .next() call genuinely resumes exactly where it left off, up to the next yield or a return/end of the function.
  • 📌 Interview term: the real, direct answer to the prompt's two-way question — verified directly: a value passed as .next(value)'s own argument genuinely becomes the evaluated result of the yield expression the generator is currently paused on — confirmed directly via real console.log lines printed from INSIDE the generator body between calls, showing the exact value received.
  • A precise answer names that a generator object genuinely has its own Symbol.iterator (verified directly), making it directly usable with for...of/spread — though for...of only reads yielded VALUES and does not support sending values back in via .next(value).

Clarifying questions expected:

  • None — this is a definitional/technical question; directly answering both the laziness and the two-way-communication parts of the prompt with real proof is the strong signal.

Code / implementation expected: Yes — a real generator driven through multiple explicit .next() calls, with console.log lines printed from inside its own body, is the clearest, most convincing demonstration of both laziness and two-way communication.

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 ordering of every log line below, including lines printed from INSIDE the

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal proof: a generator's body genuinely does not run until the first .next() call, and values passed to .next() are genuinely received inside the generator
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 133 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track