feature(util) callback(arg1...) -> callback(error, arg1...)

This commit is contained in:
coderaiser 2014-06-03 06:48:23 -04:00
parent 976c2f4e4a
commit 1ad18cb926
2 changed files with 25 additions and 16 deletions

View file

@ -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);
}
}
};

View file

@ -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);
});
});