I am learning this feature from ES6(Function generators) and I am having difficulties to understand the following code :
function* HelloGen2() {
var a = yield 100;
var b = yield a + 100;
console.log(b);
}
var gen2 = HelloGen2();
console.log(gen2.next()); // {value: 100, done: false}
console.log(gen2.next(500)); // {value: 600, done: false}
console.log(gen2.next(1000)); // {value: undefined, done: true}
Questions :
1 : In the first gen2.next() the line of code var a = yield 100; is called , is it set some value on the var a variable ?
2: In each gen2.next() only the line until the semicolon is executed ?
So for instance , in the second call gen2.next(500) the line console.log(b); is not executed
3: i do not understand the last line gen2.next(1000), how the b variable get the 1000 value ?
When working with coroutines, it's important to understand, that although yield and next are different keywords, what they do is essentially the same thing. Imagine two coroutines as being connected by a two-way communication pipe. Both
Y = yield X
and
Y = next(X)
perform the same set of operations:
write X to the pipe
wait for the answer
once the answer is there, read it from the pipe and assign it to Y
proceed with the execution
Initially, the main program is in the active state, and the generator (gen2 in your example) is waiting listening to the pipe. Now, when you call next, the main program writes a dummy value (null) to the pipe, thus waking up the generator. The generator executes yield 100, writes 100 to the pipe and waits. The main wakes up, reads 100 from the pipe, logs it and writes 500. The generator wakes up, reads 500 into a, etc. Here's the complete flow:
gen wait
main next() null -> pipe
main wait
gen pipe -> null
gen yield 100 100 -> pipe
gen wait
main pipe -> arg (100)
console.log(arg) 100
main next(500) 500 -> pipe
main wait
gen a= pipe -> a (500)
gen yield a + 100 600 -> pipe
gen wait
main pipe -> arg (600)
console.log(arg) 600
main next(1000) pipe -> 1000
main wait
gen b= pipe -> b
console.log(b) 1000
gen (ended) done -> pipe
main pipe -> arg (done)
console.log(arg)
main (ended)
Essentially, to understand generators, you have to remember that when you assign or somehow else use the result of yield/next, there's a pause between the right and the left (or "generate" and "use") parts. When you do
var a = 5
this is executed immediately, while
var a = yield 5
involves a pause between = and yield. This requires some mental gymnastics, very similar to async workflows, but with time you'll get used to that.
The relationship between yield x and generator.next(y) is as follows:
generator.next(y) returns {value: x, done: false}
yield x evaluates to y
Additionally, a generator function doesn't begin execution when initially invoked. You can think of it as a constructor. Only after generator.next() is called does the actual function code execute until the first yield statement (or the function terminates).
In your example:
var gen2 = HelloGen2() creates the generator, but nothing happnes.
gen2.next() executes the function up to yield 100 and then returns {value: 100...}.
gen2.next(500) resumes function execution, firstly evaluating the yield statement to 500 (and immediately setting this value into a), and continue execution up to yield a+100 and then returns {value: 600...}.
gen2.next(1000) resumes function execution, firstly evaluating the yield statement to 1000 (and immediately setting this value into b), and continue execution up to termination and then returns {value: undefined, done: true}.
That last undefined is the result of the function not returning any value explicitly.
Questions :
In the first gen2.next() the line of code var a = yield 100; is called, is it set some value on the var a variable?
Not at this time, with the next call of gen2.next(500) it gets the value of 500. At the first call the value is still undefined.
In each gen2.next() only the line until the semicolon is executed?
So for instance, in the second call gen2.next(500) the line console.log(b); is not executed.
Right. The function stops directly after the last yield statement.
I do not understand the last line gen2.next(1000), how the b variable get the 1000 value?
With the call of next(), the function continues at the last stop with the yield statement, the value is handed over and assigned to b.
The console.log(b)is performed and the function exits at the end and the result of the generator is set to { value: undefined, done: true }.
function* HelloGen2() {
var a, b;
console.log(a, b); // undefined undefined
a = yield 100;
console.log(a, b); // 500 undefined
b = yield a + 100;
console.log(b); // 1000
}
var gen2 = HelloGen2();
console.log('#1');
console.log(gen2.next()); // { value: 100, done: false }
console.log('#2');
console.log(gen2.next(500)); // { value: 600, done: false }
console.log('#3');
console.log(gen2.next(1000)); // { value: undefined, done: true }
console.log('#4');
.as-console-wrapper { max-height: 100% !important; top: 0; }
Related
My code works well, but I still have some questions. Why a and b declarations are not re-executed? Is only the context of while (true) scope kept? Why?
function* fibonacciGen() {
let a = 1;
let b = 1;
while (true) {
sum = a + b;
a = b;
b = sum;
yield sum;
}
}
const fibonacci = fibonacciGen();
console.log(fibonacci.next().value) // -> 2
console.log(fibonacci.next().value) // -> 3
console.log(fibonacci.next().value) // -> 5
console.log(fibonacci.next().value) // -> 8
console.log(fibonacci.next().value) // -> 13
console.log(fibonacci.next().value) // -> 21
When you call a generator function (fibonacciGen() here), the body of the function executes until it encounters a yield. So the let a and let b lines run when the generator is first called. At the yield, the function stops executing - but it's not like a return, because it'll continue when .next is called on the iterator. Calling .next tells a generator function to start executing again from the yield it last stopped at, and to continue until another yield is encountered.
Each time .next() is called, the resumed generator will finish the iteration of the while loop, then continue with the next iteration of the loop, running
sum = a + b;
a = b;
b = sum;
again - but not running the entire fibonacciGen generator function again.
Why is it that I get different results when calling next() directly on a generator, versus on a variable with the same generator assigned as its value?
All code/output below.
Below is the generator, plus variable declaration/assignment:
function* gen() {
yield 1;
yield 2;
yield 3;
};
let genVar = gen();
First code snippet:
let first = genVar.next();
console.log(first);
second = genVar.next();
console.log(second);
Output of first code snippet:
{ value: 1, done: false }
{ value: 2, done: false }
Second code snippet:
let one = gen().next();
console.log(one);
two = gen().next();
console.log(two);
Output of second code snippet:
{ value: 1, done: false }
{ value: 1, done: false }
My best guess at the moment is this has something to do with assignment by value/reference?
Anytime you execute the generator function, you create a brand new generator object that allows to iterate over the steps of the generator function from the start.
So in your second example, you actually create two different iterators.
I can not understand why #1 and #2 produce different results.
To my basic understanding 4,4,4,4 is correct, since callbacks exist in the callback queue (you can check this at http://latentflip.com/loupe/).
//#1
function func1(a,b,func){
var k = a + b;
for (var i =0;i<k;i++){
func(i);
}
}
function func2(param){
setTimeout(function() {
console.log(param);
}, 10);
}
func1(1,3,function(result){
func2(result);
});
result of above #1 >>> 0, 1, 2 ,3
//#2
function func1(a,b){
var k = a + b;
for (var i =0;i<k;i++){
setTimeout(function() {
console.log(i);
}, 10);
}
}
func1(1,3);
result of above #2 >>> 4, 4, 4, 4
In the second example you are calling the wait function within the for loop itself, which causes the JavaScript to execute the queued console.log commands only after the value of i has finished incrementing.
In the first example, the function with the log statement is called externally, and only returns to the for loop once the output has been printed. The callback correctly prevents further execution of the loop until each call to the external function has been completed.
The first function prints the i var.
The second one increases i while it keeps going in the for loop, and when the timeout ends i=4, so it prints 4 times 4.
This because it is an async function.
The right way to do what you want to is the first one, calling another function to force the console.log(i) and not keep going until the func2 ends.
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 foo1 and pause at yield (and the next call which returns foo1 takes as argument 42)
pause until next call to next
on next yield proceed to the line with var x = 1 + 42 because this was the argument previously received
print x = 43
just return a {done: true} from the last next, ignoring its argument (43) and stop.
Now, obviously, this is not what's happening. So... what am I getting wrong here?
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...
I got this from Axel Rauschmayer's Exploring ES6, especially 22.4.1.1.
On receiving a .next(arg), the first action of a generator is to feed arg to yield. But on the first call to .next(), there is no yield to receive this, since it is only at the end of the execution.
Only on the second invocation x = 1 + 43 is executed and subsequently logged, and the generator ends.
Also had a hard time wrapping my head around generators, especially when throwing in if-statements depended on yielded values. Nevertheless, the if-statement was actually what helped me getting it at last:
function* foo() {
const firstYield = yield 1
console.log('foo', firstYield)
const secondYield = yield 3
console.log('foo', secondYield)
if (firstYield === 2) {
yield 5
}
}
const generator = foo()
console.log('next', generator.next( /* Input skipped */ ).value)
console.log('next', generator.next(2).value)
console.log('next', generator.next(4).value)
/*
Outputs:
next 1
foo 2
next 3
foo 4
next 5
*/
Everything immediately became clear once I made this realization.
Here's your typical generator:
function* f() {
let a = yield 1;
// a === 200
let b = yield 2;
// b === 300
}
let gen = f();
gen.next(100) // === { value: 1, done: false }
gen.next(200) // === { value: 2, done: false }
gen.next(300) // === { value: undefined, done: true }
But here's what actually happens. The only way to make generator execute anything is to call next() on it. Therefore there needs to be a way for a generator to execute code that comes before the first yield.
function* f() {
// All generators implicitly start with that line
// v--------<---< 100
= yield
// ^-------- your first next call jumps right here
let a = yield 1;
// a === 200
let b = yield 2;
// b === 300
}
In node (0.11.9, with the --harmony flag), how do I restart a generator after it finishes?
I tried doing generator.send(true); but it says the send() method doesn't exists.
You don't restart a generator. Once it has completed, it has finished its run like any other function. You need to recreate the generator to run again.
var count = function*(){ yield 1; return 2;};
var gen = count();
var one = gen.next();
var two = gen.next();
// To run it again, you must create another generator:
var gen2 = count();
The other option would be to design your generator such that it never finishes, so you can continue calling it forever. Without seeing the code you are talking about, it is hard to make suggestions though.
A bit late, but this is just a FYI.
At the moment, the send method is not implemented in Node, but is in Nightly (FF) - and only in some way.
Nightly:
If you declare your generator without the *, you'll get an iterator that has a send method:
var g = function() {
var val = yield 1; // this is the way to get what you pass with send
yield val;
}
var it = g();
it.next(); // returns 1, note that it returns the value, not an object
it.send(2); // returns 2
Node & Nightly:
Now, with the real syntax for generators - function*(){} - the iterators you produce won't have a send method. BUT the behavior was actually implemented in the next method. Also, note that it was never intended for send(true); to automatically restart your iterator. You have to test the value returned by yield to manually restart it (see the example in the page you linked). Any value, as long as it's not a falsy one, could work. See for yourself:
var g = function*() {
var val = 1;
while(val = yield val);
}
var it = g();
it.next(); // {done: false, value: 1}
it.next(true); // {done: false, value: true}
it.next(2); // {done: false, value: 2}
it.next(0); // {done: true, value: undefined}