Full answer
Walk through each part:
- First loop: gen() creates the generator but does not run it. The first next() from for await starts the body, which prints start and yields 1 (got 1), then 2 (got 2). The break calls the generator's return() method. That resumes the generator at the yield and runs the finally block, so cleanup is printed before the loop is finished.
- const g = gen(); console.log('created'): no start yet. Creating an async generator does not run any of its code.
- await g.next() starts the body: start is printed and the result is { value: 1, done: false }.
- await g.return('early') resumes the generator with a return completion. The finally block runs (cleanup) and the result is { value: 'early', done: true }.
- Another next() on a finished generator gives { value: undefined, done: true } and prints nothing.
- for await over an array with a promise, a plain value and a promise that resolves later: for await awaits each item, so it prints a, b, c.
- h.next() is called twice without waiting. An async generator keeps a queue of requests and answers them in order. The first call starts the body (start), so the output is start and then 1 2 from the log. The final h.return() finishes the generator and runs cleanup.
Remember: finally in a generator runs when the consumer leaves early with break, return or throw, as long as the generator was started. If a generator was never started, return() does not run its body, so there is nothing to clean up.
javascript
async function* gen() {
try { console.log('start'); yield 1; yield 2; yield 3; }
finally { console.log('cleanup'); }
}
(async () => {
for await (const v of gen()) { console.log('got', v); if (v === 2) break; }
const g = gen();
console.log('created');
console.log(await g.next());
console.log(await g.return('early'));
console.log(await g.next());
for await (const v of [Promise.resolve('a'), 'b', wait('c')]) console.log(v);
const h = gen();
const [p1, p2] = [h.next(), h.next()];
console.log((await p1).value, (await p2).value);
await h.return();
})();
function wait(v) { return new Promise((r) => setTimeout(r, 10, v)); }The snippet is the puzzle itself. The output is the order of the log lines. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, run once): start got 1 got 2 cleanup created start { value: 1, done: false } cleanup { value: 'early', done: true } { value: undefined, done: true } a b c start 1 2 cleanup
Key points
- The body of a generator starts at the first next(), not at the call.
- break in for await calls return(), which runs finally.
- return() on a started generator runs finally and completes it.
- Queued next() calls are answered in order.
- for await also works on iterables of promises and plain values.
Common mistakes
- Expecting start to print when gen() is called.
- Expecting cleanup to be skipped on break.
- Thinking an already finished generator throws on next().
- Thinking for await requires an async iterable only.
- Forgetting that the 10 ms timer for c does not change the order.
Follow-ups
- What happens if the consumer throws inside the for await body?
- How does an async generator relate to a Readable stream?
- Why do streams need cleanup logic like finally?
Tips
- Say 'lazy until the first next'.
- Mention break calling return.
- Explain the request queue.