In the code below, why does the first call to next()
return the value that comes after the second yield
keyword?
function* generatorFunction() {
yield (yield 'the second yield')();
}
function func(x) {
return 'func passed to the second next()';
}
var iterator = generatorFunction();
var firstValue = iterator.next().value;
var secondValue = iterator.next(func).value;
console.log(firstValue); // "the second yield"
console.log(secondValue); // "func passed to the second next()"
On the first next()
it treats the rest of the line after the first yield
as an expression, right? Does that mean (yield x)()
evaluates to x, but doesn't actually pause on the yield while it's evaluating it?
Then the second call to next
passes in a function, does that function take the place of (yield 'the second yield')
and so get executed and then return its value.
Can anyone help explain what's going on here?
BTW this serves no practical purpose, I'm just going through the es6katas.