Open mor-n4 opened 3 years ago
Mind the example below copied from 2ality.com.
Code
function* genFuncWithReturn() {
yield 'a';
yield 'b';
return 'result';
}
Output
> let genObjWithReturn = genFuncWithReturn();
> genObjWithReturn.next()
{ value: 'a', done: false }
> genObjWithReturn.next()
{ value: 'b', done: false }
> genObjWithReturn.next()
{ value: 'result', done: true }
Mind that loops etc. ignore the last entry (done == true).
for (let x of genFuncWithReturn()) {
console.log(x);
}
// Output:
// a
// b
let arr = [...genFuncWithReturn()]; // ['a', 'b']
Method
Generator#next()
is declared asbut this seems to be incorrect, because the value in the IteratorEntry returned by
#next()
might be the generator's return value.To reproduce:
Probably the signature should be: