Has anyone tried to get Underscore JS or lodash (or any ES5 standard functions for that matter) working with generators?
If we have an array var myArray = [1,2,3,4,6];
We want to forEach over it.
In a non generator case you would simply
myArray.forEach(function(k) {
console.log(k);
});
However, when you can't yield inside a non generator function, so if inside this loop we had to do some async work, you would need to do the following.
var foreach = function* (arr, fn) {
var i;
for (i = 0; i < arr.length; i++) {
yield * fn(arr[i], i);
}
};
yield* foreach(myArray, function* (k) {
var a = yield fs.readFile();
});
Which kind of sucks.
Anyone know of a way to get anonymous functions working with generators? We kind of lose the entire lodash library because of this.
Note: I'm using Traceur to compile my code into ES6 with generators turned on.
Note: I'm not using co(). I'm using a custom generator function seen below
var run = function(generatorFunction) {
var generatorItr = generatorFunction(resume);
function resume(callbackValue) {
generatorItr.next(callbackValue);
}
generatorItr.next();
};