Emmanuel Akinfulubi

JavaScriptFundamental (ES6), part 2

Pass values into a generator with next() in JavaScript

2 Send values back in with next(value)

hard

Build a running-total generator where each next(amount) feeds an amount in and gets the new total out. Then explain what happened to the argument of the first next().

Stuck? Three hints, smallest first
  1. 1. const x = yield y sends y out and, when resumed, evaluates to whatever was passed to next().
  2. 2. The first next() only starts the body running. There is no suspended yield waiting to receive its argument.
  3. 3. That is why libraries "prime" a generator with one bare next() before using it.

The answer

function* runningTotal() {
  let total = 0;
  while (true) {
    const amount = yield total;   // out goes the total, in comes the next amount
    total += amount;
  }
}

const till = runningTotal();
const first = till.next(500);     // 500 is DISCARDED: no yield is waiting for it
const second = till.next(1000);
const third = till.next(250);

// The fix is to prime it, then hide the priming behind a small API.
function openTill() {
  const g = runningTotal();
  g.next();                       // run to the first yield and drop that value
  return { add: (naira) => g.next(naira).value };
}
const shop = openTill();
const takings = [shop.add(4500), shop.add(300), shop.add(1200)];

const sentNothing = (() => { const g = runningTotal(); g.next(); return g.next().value; })();

console.log('first next(500) gave', first.value, '<- the 500 went nowhere');
console.log('then next(1000) ->', second.value, ' next(250) ->', third.value);
console.log('primed till     :', takings);
console.log('next() with no argument:', sentNothing);

Output


      
7 checks that prove this answer

You do not have to read these. They are part of the answer panel above, so pressing Run there executes all 7 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

7 checks

test('the first next only starts the body', first.value, 0);
test('so its argument never arrives', first.value === 500, false);
test('the second next really does send a value in', second.value, 1000);
test('and the amounts accumulate', third.value, 1250);
test('priming it first makes the API read naturally', takings, [4500, 4800, 6000]);
test('next() with no argument sends undefined, so the sum goes NaN', Number.isNaN(sentNothing), true);
test('a generator with no return is never done', till.next(1).done, false);

yield is an expression, not a statement, and that is the whole exercise. It has a value on resumption, and that value is the argument of the next() that resumed it. Two-way communication with no callbacks.

The discarded first argument catches everyone. When you call g.next(500) on a fresh generator, the body has not started, so there is no suspended yield expression for 500 to become. It is silently dropped — no error, no warning. Priming with a bare next() is the standard fix, and wrapping the generator so callers cannot forget is better than documenting it.

The NaN assertion is the failure mode in production: one next() with a missing argument and every total after it is NaN, because NaN is contagious through arithmetic and never throws.

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.