I came across generator functions on MDN , and that puzzles me with the following example:
function* logGenerator() { console.log(yield); console.log(yield); console.log(yield); } var gen = logGenerator();
I donβt understand why the yield , which is an argument to console.log , returns the parameter passed to the .next() method. Is this because an empty yield should return the value of the first parameter of the .next() method?
I also tried some more examples that seem to confirm the above statement:
gen.next(1,2,3); // the printed value is 1, the 2 and 3 are ignored // and the actual yielded value is undefined
Is there also a way to access additional parameters of the .next() method inside the body of the generator function?
Another thing I noticed is that while the yield statement returns these values ββin console.log , they are not actually output as generator output. I have to say, I find this very confusing.
Jakub source share