I am trying to understand how the heck this is working. I have manipulated this code inside out and still don't understand how it is returning the values it is. I got this from a tutorial at http://davidwalsh.name/es6-generators
function *foo(x) {
var y = 2 * (yield (x + 1));
var z = yield (y / 3);
return (x + y + z);
}
var it = foo( 5 );
// note: not sending anything into `next()` here
console.log( it.next() ); // { value:6, done:false }
console.log( it.next( 12 ) ); // { value:8, done:false }
console.log( it.next( 13 ) ); // { value:42, done:true }
I seem to have somewhat of a grasp on the first next(), but the next 2 baffle me with the value of 8 and 42.
Hope someone can help explain this so I can try to grasp and move onto the next stages with the generators.