When passing parameters to next() of ES6 generators, why is the first value ignored? More concretely, why does the output of this say x = 44 instead of x = 43:
function* foo() {
let i = 0;
var x = 1 + (yield "foo" + (++i));
console.log(`x = ${x}`);
}
fooer = foo();
console.log(fooer.next(42));
console.log(fooer.next(43));
// output:
// { value: 'foo1', done: false }
// x = 44
// { value: undefined, done: true }
My mental model for the behavior of such a generator was something like:
- return
foo1and pause at yield (and thenextcall which returnsfoo1takes as argument42) - pause until next call to
next - on next yield proceed to the line with
var x = 1 + 42because this was the argument previously received - print
x = 43 - just return a
{done: true}from the lastnext, ignoring its argument (43) and stop.
Now, obviously, this is not what's happening. So... what am I getting wrong here?