[Pkg-javascript-commits] [node-async] 08/480: added parallel and series methods
Jonas Smedegaard
js at moszumanska.debian.org
Fri May 2 08:58:07 UTC 2014
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository node-async.
commit bf0dfd86bcd44ad086bb01ee15c210f2c508c596
Author: Caolan McMahon <caolan at caolanmcmahon.com>
Date: Mon May 17 17:08:28 2010 +0100
added parallel and series methods
---
lib/async.js | 30 ++++++++++++++++++++++++++++++
test/test-async.js | 36 ++++++++++++++++++++++++++++++++++++
2 files changed, 66 insertions(+)
diff --git a/lib/async.js b/lib/async.js
index 3634bf2..faac869 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -63,3 +63,33 @@ exports.waterfall = function(arr){
};
makeCallback(0)();
};
+
+exports.parallel = function(arr, callback){
+ var results = [];
+ arr.forEach(function(fn){
+ fn(function(){
+ var args = Array.prototype.slice.call(arguments);
+ results.push((args.length > 1) ? args: args[0]);
+ if(results.length == arr.length){
+ callback(results);
+ }
+ });
+ });
+};
+
+exports.series = function(arr, callback){
+ var results = [];
+ var makeCallback = function(index){
+ return function(){
+ if(index > 0){
+ var args = Array.prototype.slice.call(arguments);
+ results.push((args.length > 1) ? args: args[0]);
+ }
+ if(index < arr.length){
+ arr[index](makeCallback(index+1));
+ }
+ else callback(results);
+ }
+ };
+ makeCallback(0)();
+};
diff --git a/test/test-async.js b/test/test-async.js
index b1cffdf..1451190 100644
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -128,3 +128,39 @@ exports.testWaterfallMultipleCallback = function(test){
];
async.waterfall(arr);
};
+
+exports.testParallel = function(test){
+ async.parallel([
+ function(callback){
+ setTimeout(function(){callback(1);}, 100);
+ },
+ function(callback){
+ setTimeout(function(){callback(2);}, 200);
+ },
+ function(callback){
+ setTimeout(function(){callback(3,3);}, 50);
+ }
+ ],
+ function(results){
+ test.same(results, [[3,3],1,2]);
+ test.done();
+ });
+};
+
+exports.testSeries = function(test){
+ async.series([
+ function(callback){
+ setTimeout(function(){callback(1);}, 100);
+ },
+ function(callback){
+ setTimeout(function(){callback(2);}, 200);
+ },
+ function(callback){
+ setTimeout(function(){callback(3,3);}, 50);
+ }
+ ],
+ function(results){
+ test.same(results, [1,2,[3,3]]);
+ test.done();
+ });
+};
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-async.git
More information about the Pkg-javascript-commits
mailing list