From 1ad18cb92637e39fd74d50dcbe2b2f33a72448b6 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Tue, 3 Jun 2014 06:48:23 -0400 Subject: [PATCH] feature(util) callback(arg1...) -> callback(error, arg1...) --- lib/util.js | 35 ++++++++++++++++++++++------------- test/lib/util.js | 6 +++--- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/lib/util.js b/lib/util.js index 7277e6ec..b852d6a4 100644 --- a/lib/util.js +++ b/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); + } } }; diff --git a/test/lib/util.js b/test/lib/util.js index 7a2ec0e8..cabd4112 100644 --- a/test/lib/util.js +++ b/test/lib/util.js @@ -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); }); });