mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-20 18:18:56 +00:00
feature(util) callback(arg1...) -> callback(error, arg1...)
This commit is contained in:
parent
976c2f4e4a
commit
1ad18cb926
2 changed files with 25 additions and 16 deletions
35
lib/util.js
35
lib/util.js
|
|
@ -756,28 +756,37 @@
|
|||
};
|
||||
|
||||
exec.parallel = function(funcs, callback) {
|
||||
var count = 0,
|
||||
allData = [],
|
||||
var errorWas,
|
||||
count = 0,
|
||||
allData = [null],
|
||||
func = exec.ret(callback),
|
||||
funcsCount = funcs.length;
|
||||
|
||||
funcs.forEach(function(func, num) {
|
||||
exec(func, function() {
|
||||
checkFunc(num, arguments);
|
||||
checkFunc(num + 1, arguments);
|
||||
});
|
||||
});
|
||||
|
||||
function checkFunc(num, data) {
|
||||
var length = data.length;
|
||||
var args = Util.slice(data, 1),
|
||||
error = data[0],
|
||||
length = args.length;
|
||||
|
||||
++count;
|
||||
|
||||
if (length >= 2) {
|
||||
allData[num] = data;
|
||||
} else
|
||||
allData[num] = data[0];
|
||||
|
||||
if (count === funcsCount)
|
||||
exec.ret(callback).apply(null, allData);
|
||||
if (error) {
|
||||
errorWas = true;
|
||||
func(error);
|
||||
} else if (!errorWas) {
|
||||
++count;
|
||||
|
||||
if (length >= 2)
|
||||
allData[num] = args;
|
||||
else
|
||||
allData[num] = args[0];
|
||||
|
||||
if (count === funcsCount)
|
||||
func.apply(null, allData);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -100,16 +100,16 @@
|
|||
var WORD = 'hello world',
|
||||
funcSlow = function(callback) {
|
||||
setTimeout(function() {
|
||||
Util.exec(callback, 'hello');
|
||||
callback(null, 'hello');
|
||||
}, 10);
|
||||
},
|
||||
funcFast = function(callback) {
|
||||
setTimeout(function() {
|
||||
Util.exec(callback, 'world');
|
||||
callback(null, 'world');
|
||||
}, 1);
|
||||
};
|
||||
|
||||
Util.exec.parallel([funcSlow, funcFast], function(hello, world) {
|
||||
Util.exec.parallel([funcSlow, funcFast], function(error, hello, world) {
|
||||
WORD.should.equal(hello + ' ' + world);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue