Run generator function as forEach loop callback
npm install co-foreach
var forEach = require('co-foreach');
// Every generator callback is wrapped into co function,
// so you can take advantage of all co features
forEach(array, function * (item, idx) {
// do something awesome with generators
}).then(handleFinish);
var forEach = require('co-foreach');
var Q = require('q');
var fs = require('fs');
var files = ['./test/test1.txt', './test/test2.txt'];
// co-foreach is returning promise
forEach(files, function * (file, idx) {
var content = yield Q.nfcall(fs.readFile, file);
// do something usefull
}).then(function () {
// co-foreach is returning promise which is fulfilled
// after all generator functions are successfully finished
}, function (err) {
// handle error
});
var forEach = require('co-foreach');
// You can also use co-foreach with normal callbacks
forEach(array, function (item, idx) {
});
MIT