Reputation: 21599
If I have a following generator:
function* generate() {
yield 1;
yield 2;
return 3;
}
Is there something built in in ES6 to get [1, 2]
and 3
out of it (without doing next
manually)?
If I only needed 1
and 2
I could use for(of)
, but I want to get 3
as well.
Upvotes: 0
Views: 162
Reputation: 159
Generators are not designed to work that way. If you want to get all the values at once, then I hardly see the point of using generators. If you must, you can do something like this -
function* generate() {
yield 1;
yield 2;
return 3;
}
var arr = [], k, it = generate();
do {
k = it.next();
arr.push(k.value);
} while (!k.done);
console.log(arr);
Upvotes: 1