I'm trying to have a better understanding of how generators work in javascript.
From MDN:
The function* declaration (function keyword followed by an asterisk) defines a generator function, which returns a Generator object.
function *range(from, to) {
var counter = from;
while(to >= counter) {
yield counter
counter++
}
}
for (var r of range(5, 10)) {
console.log( r );
}
// print: 5, 6, 7, 8, 9, 10
I'm not sure how I understand exactly what's happening in the snippet above.
Aren't generators supposed to be called, stored as (generator) objects and then called by the next() method. (like below)
function *foo () {
yield 'woo';
}
var G = foo();
console.log( G.next() );
In the code above, on line 4 with var G = foo(); I am not calling a function and creating new execution context, this should just return a generator object (and store it under the label G).
I'll invoke the actual function foo, when I call the next() method on it, on line 5. At that point, I'm creating an execution context, executing the code inside foo and yielding out the string "woo".
How the first snippet is exactly supposed to work?