🟑 MediumEvent Loop & PromisesπŸ’» Output Question

Generator execution order

πŸ’‘

Hint

Generators are lazy: code only runs when you call next(). Each yield is a pause point. The generator resumes exactly where it left off.

What does this output?

function* gen() {
  console.log('A');
  yield 1;
  console.log('B');
  yield 2;
  console.log('C');
}

const g = gen();
console.log(g.next().value);
console.log(g.next().value);
console.log(g.next().done);

Correct Output

A
1
B
2
C
true

Why this output?

Explanation: gen() does NOT run yet β€” just creates the iterator. First next(): runs until yield 1, logs A, returns {value:1}. Second next(): resumes, logs B, returns {value:2}. Third next(): resumes, logs C, hits end, returns {done:true}.

Key Insight: Generators are lazy: code only runs when you call next(). Each yield is a pause point. The generator resumes exactly where it left off.

More Event Loop & Promises Output Questions

🟑 MediumClassic setTimeout vs Promiseβ†’πŸ”΄ HardNested setTimeout and Promiseβ†’πŸŸ‘ Mediumasync/await execution orderβ†’πŸ”΄ HardPromise chaining orderβ†’

Practice predicting output live β†’

66 output questions with instant feedback

πŸ’» Try Output Quiz