Skip to the solution
HardFrontend6 min read

What is a generator function?

806 views

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

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: a generator's body genuinely does not run until the first .next() call, and values passed to .next() are genuinely received inside the generator

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 133 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

How would you implement an async generator function to stream paginated API results, and how does a consumer use it with for await...of?

Continue