mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-18 00:47:01 +00:00
feature(util) exec: each, eachSeries
This commit is contained in:
parent
ab052f52f0
commit
c500e7a4c6
1 changed files with 49 additions and 12 deletions
61
lib/util.js
61
lib/util.js
|
|
@ -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);
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue