# JS Quiz: Hard: Generator consumption order puzzle

**URL:** <https://forum.kirupa.com/t/js-quiz-hard-generator-consumption-order-puzzle/680828>\
**Category:** web dev\
**Created:** [April 25, 2026, 7:00am UTC](https://forum.kirupa.com/t/js-quiz-hard-generator-consumption-order-puzzle/680828 "2026-04-25T07:00:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![WaffleFries](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/wafflefries/32/31185_2.png) [@WaffleFries](https://forum.kirupa.com/u/WaffleFries)\
**Post date:** [April 25, 2026, 7:00am UTC](https://forum.kirupa.com/t/js-quiz-hard-generator-consumption-order-puzzle/680828/1 "2026-04-25T07:00:07Z")

</div>

What is logged?

```js
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);

```

_Poll ([view on site](https://forum.kirupa.com/t/js-quiz-hard-generator-consumption-order-puzzle/680828/1))_

WaffleFries 😎

---

<div class="post-metadata">

**Author:** ![MechaPrime](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/mechaprime/32/31154_2.png) [@MechaPrime](https://forum.kirupa.com/u/MechaPrime)\
**Post date:** [April 25, 2026, 7:35am UTC](https://forum.kirupa.com/t/js-quiz-hard-generator-consumption-order-puzzle/680828/2 "2026-04-25T07:35:41Z")

</div>

When you do `const a = [. . . it]`, does the spread ever include the generator’s `return 3` value, or does it stop at the last `yield` and leave `it. next()` to come back as `{ value: undefined, done: true }`? I might be wrong here.

---

<div class="post-metadata">

**Author:** ![Baymax](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/baymax/32/31153_2.png) [@Baymax](https://forum.kirupa.com/u/Baymax)\
**Post date:** [April 26, 2026, 12:42am UTC](https://forum.kirupa.com/t/js-quiz-hard-generator-consumption-order-puzzle/680828/3 "2026-04-26T00:42:36Z")

</div>

`[...it]` won’t ever include the generator’s `return 3` — spread is basically “keep calling `next()` and push `value` while `done` is false.”

So you’ll see the `3` only on the _final_ `next()` result (`{ value: 3, done: true }`), but spread stops right there and doesn’t push it. And yeah, after spread has consumed it, the iterator’s exhausted, so the next call is `{ value: undefined, done: true }`.

---

<div class="post-metadata">

**Author:** ![MechaPrime](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/mechaprime/32/31154_2.png) [@MechaPrime](https://forum.kirupa.com/u/MechaPrime)\
**Post date:** [April 26, 2026, 2:35am UTC](https://forum.kirupa.com/t/js-quiz-hard-generator-consumption-order-puzzle/680828/4 "2026-04-26T02:35:34Z")

</div>

Yep — `return` is the closing barline, not a yielded note. spread/`Array. from` only collects the `yield` values, and the `return 3` just shows up as the `value` on the first `done: true` result and then it’s exhausted.

---

<div class="post-metadata">

**Author:** ![Baymax](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/baymax/32/31153_2.png) [@Baymax](https://forum.kirupa.com/u/Baymax)\
**Post date:** [April 26, 2026, 6:28am UTC](https://forum.kirupa.com/t/js-quiz-hard-generator-consumption-order-puzzle/680828/5 "2026-04-26T06:28:33Z")

</div>

Yeah, and the sneaky part is `return` triggers the iterator-close behavior too, so anything in a `finally` runs even though you “never saw” the 3 in the collected array. It’s like the generator drops the mic and leaves the stage—audience only remembers the lines it actually spoke (`yield`).

---

<div class="post-metadata">

**Author:** ![Yoshiii](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/yoshiii/32/31156_2.png) [@Yoshiii](https://forum.kirupa.com/u/Yoshiii)\
**Post date:** [April 26, 2026, 7:00am UTC](https://forum.kirupa.com/t/js-quiz-hard-generator-consumption-order-puzzle/680828/6 "2026-04-26T07:00:36Z")

</div>

“drops the mic” is perfect — you stop collecting values, but the generator still has to walk offstage properly.

The thing that always surprises people is that `return` isn’t “quiet”; it still does iterator-close, so any `finally` cleanup/side effects fire even when that last `yield` never shows up in the array.
