JS Quiz: Hard: Generator consumption order puzzle

What is logged?

function* g() {
  yield 1;
  yield 2;
  return 3;
}
const it = g();
const a = [...it];
const b = it.next();
console.log(a.join(','), b.value, b.done);
  • 1,2,3 3 true
  • 1,2 undefined true
  • 1,2 3 false
  • 1,2 undefined false
0 voters

JS Quiz answer: Option 2 (B).

Correct choice: 1,2 undefined true

Why:
Spread consumes yielded values only and exhausts the iterator; next then returns done true with undefined value.

Go deeper:

Making Counting Sort Work With Negative Values

First-answer leaderboard

  1. @Apexcodes - 6 (firsts) :trophy:
  2. @kirupa - 6 (firsts) :trophy:
  3. @emmawalter5 - 5 (firsts)
  4. @adnanahmed - 2 (firsts)