feature(util) exec: each, eachSeries

This commit is contained in:
coderaiser 2015-07-21 04:32:24 -04:00
parent ab052f52f0
commit c500e7a4c6

View file

@ -395,19 +395,56 @@
* load functions thrue callbacks one-by-one
* @param funcs {Array} - array of functions
*/
exec.series = function(funcs) {
var func, callback,
isArray = Util.type.array(funcs);
if (isArray) {
func = funcs.shift();
callback = function() {
return exec.series(funcs);
exec.series = function(funcs, callback) {
var fn,
i = funcs.length,
check = function(error) {
var done;
--i;
if (!i || error) {
done = true;
exec(callback, error);
}
return done;
};
exec(func, callback);
}
if (!Array.isArray(funcs))
throw(Error('funcs should be array!'));
fn = funcs.shift();
exec(fn, function(error) {
if (!check(error))
exec.series(funcs, callback);
});
};
exec.each = function(array, iterator, callback) {
var listeners = array.map(function(item) {
return iterator.bind(null, item);
});
if (!listeners.length)
callback();
else
exec.parallel(listeners, callback);
};
exec.eachSeries = function(array, iterator, callback) {
var listeners = array.map(function(item) {
return iterator.bind(null, item);
});
if (typeof callback !== 'function')
throw Error('callback should be function');
if (!listeners.length)
callback();
else
exec.series(listeners, callback);
};
/**