Say we have this generator function (generator fn returns an iterator):
function* foo(){
yield 1;
yield 2;
yield 3;
}
for (let i of foo()) {
console.log(i);
}
we can iterate over the iterator using a for..of
loop.
My question is: is there a way to create a functional for-loop to iterate over an iterator like with an Array?
[1,2,3].forEach(function(i){
// we have our own block scope for each item in the iterable
});
is our best bet this technique?
for (let i of foo()) {
(function(i){
})(i);
}
seem like there must be an easier way