I am having a hard time reconciling these two:
const gen = function *() {
yield 3;
yield 4;
return 5;
};
const rator = gen();
console.log(rator.next()); // { value: 3, done: false }
console.log(rator.next()); // { value: 4, done: false }
console.log(rator.next()); // { value: 5, done: true }
The above we see all 3 values, if we call next() a fourth time, we get:
{ value: undefined, done: true }
which makes sense. But now if we use it in a loop:
for(let v of gen()){
console.log('next:', v); // next: 3, next: 4
}
I guess I am confused why using the for-loop doesn't print next: 5
, but calling next() on the iterator manually can get the return
value. Can anyone explain why this is?
In other words, I would expect the for loop
to print next: 5
but it doesn't.