I ended up writing this kind of code to investigate the behavior more thoroughly (after re-re-...-reading the MDN docs on generators):
function* bar() {
pp('in bar');
console.log(`1. ${yield 100}`);
console.log(`after 1`);
console.log(`2. ${yield 200}`);
console.log(`after 2`);
}
let barer = bar();
pp(`1. next:`, barer.next(1));
pp(`--- done with 1 next(1)\n`);
pp(`2. next:`, barer.next(2));
pp(`--- done with 2 next(2)\n`);
pp(`3. next:`, barer.next(3));
pp(`--- done with 3 next(3)\n`);
which outputs this:
in bar
1. next: { value: 100, done: false }
--- done with 1 next(1)
1. 2
after 1
2. next: { value: 200, done: false }
--- done with 2 next(2)
2. 3
after 2
3. next: { value: undefined, done: true }
--- done with 3 next(3)
So apparently the correct mental model would be like this:
on first call to next
, the generator function body is run up to the yield
expression, the "argument" of yield
(100
the first time) is returned as the value returned by next
, and the generator body is paused before evaluating the value of the yield expression -- the "before" part is crucial
only on the second call to next
is the value of the first yield
expression computed/replaced with the value of the argument given to next on this call (not with the one given in the previous one as I expected), and execution runs until the second yield
, and next
returns the value of the argument of this second yield -- here was my mistake: I assumed the value of the first yield
expression is the argument of the first call to next
, but it's actually the argument of the second call to next
, or, another way to put it, it's the argument of the call to next
during whose execution the value is actually computed
This probably made more sense to who invented this because the # of calls to next
is one more times the number of yield
statements (there's also the last one returning { value: undefined, done: true }
to signal termination), so if the argument of the first call would not have been ignored, then the one of the last call would have had to be ignored. Also, while evaluating the body of next, the substitution would have started with the argument of its previous invocation. This would have been much more intuitive imho, but I assume it's about following the convention for generators in other languages too and consistency is the best thing in the end...
Off-topic but enlightening: Just tried to do the same exploration in Python, which apparently implements generators similar to Javascript, I immediately got a TypeError: can't send non-None value to a just-started generator
when trying to pass an argument to the first call to next()
(clear signal that my mental model was wrong!), and the iterator API also ends by throwing a StopIteration
exception, so no "extra" next()
needed just to check if the done
is true (I imagine using this extra call for side effects that utilize the last next argument would only result in very hard to understand and debug code...). Much easier to "grok it" than in JS...
foo()
does not execute until the firstyield
, it only creates the generator object. The firstnext()
call starts execution from the top of the function till the firstyield
- and returns the value yielded there. – Gulgeenext
method? – Gulgee