[Pkg-javascript-commits] [node-async] 48/480: allow parallel and series functions to accept and object instead of an array
Jonas Smedegaard
js at moszumanska.debian.org
Fri May 2 08:58:11 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 641cb768938756997c9720aa1dadd0975fab1ee9
Author: Caolan McMahon <caolan at caolanmcmahon.com>
Date: Sun Oct 17 18:59:07 2010 +0100
allow parallel and series functions to accept and object instead of an array
---
README.md | 61 ++++++++++++++++++++++++++++++++++++++++++++----
lib/async.js | 54 +++++++++++++++++++++++++++++++++----------
test/test-async.js | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 166 insertions(+), 17 deletions(-)
diff --git a/README.md b/README.md
index 168b205..657a69d 100644
--- a/README.md
+++ b/README.md
@@ -444,12 +444,19 @@ Same as async.concat, but executes in series instead of parallel.
Run an array of functions in series, each one running once the previous
function has completed. If any functions in the series pass an error to its
callback, no more functions are run and the callback for the series is
-immediately called with the value of the error.
+immediately called with the value of the error. Once the tasks have completed,
+the results are passed to the final callback as an array.
+
+It is also possible to use an object instead of an array. Each property will be
+run as a function and the results will be passed to the final callback as an object
+instead of an array. This can be a more readable way of handling results from
+async.series.
+
__Arguments__
-* tasks - An array of functions to run, each function is passed a callback it
- must call on completion.
+* tasks - An array or object containing functions to run, each function is passed
+ a callback it must call on completion.
* callback(err, results) - An optional callback to run once all the functions
have completed. This function gets an array of all the arguments passed to
the callbacks used in the array.
@@ -472,6 +479,24 @@ __Example__
});
+ // an example using an object instead of an array
+ async.series({
+ one: function(callback){
+ setTimeout(function(){
+ callback(null, 1);
+ }, 200);
+ },
+ two: function(callback){
+ setTimeout(function(){
+ callback(null, 2);
+ }, 100);
+ },
+ },
+ function(err, results) {
+ // results is now equals to: {one: 1, two: 2}
+ });
+
+
---------------------------------------
<a name="parallel" />
@@ -480,11 +505,19 @@ __Example__
Run an array of functions in parallel, without waiting until the previous
function has completed. If any of the functions pass an error to its
callback, the main callback is immediately called with the value of the error.
+Once the tasks have completed, the results are passed to the final callback as an
+array.
+
+It is also possible to use an object instead of an array. Each property will be
+run as a function and the results will be passed to the final callback as an object
+instead of an array. This can be a more readable way of handling results from
+async.parallel.
+
__Arguments__
-* tasks - An array of functions to run, each function is passed a callback it
- must call on completion.
+* tasks - An array or object containing functions to run, each function is passed a
+ callback it must call on completion.
* callback(err, results) - An optional callback to run once all the functions
have completed. This function gets an array of all the arguments passed to
the callbacks used in the array.
@@ -511,6 +544,24 @@ __Example__
});
+ // an example using an object instead of an array
+ async.parallel({
+ one: function(callback){
+ setTimeout(function(){
+ callback(null, 1);
+ }, 200);
+ },
+ two: function(callback){
+ setTimeout(function(){
+ callback(null, 2);
+ }, 100);
+ },
+ },
+ function(err, results) {
+ // results is now equals to: {one: 1, two: 2}
+ });
+
+
---------------------------------------
<a name="whilst" />
diff --git a/lib/async.js b/lib/async.js
index e557085..9593fb4 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -349,28 +349,58 @@
async.parallel = function(tasks, callback){
callback = callback || function(){};
- async.map(tasks, function(fn, callback){
- if(fn){
- fn(function(err){
+ if (tasks.constructor === Array) {
+ async.map(tasks, function(fn, callback){
+ if(fn){
+ fn(function(err){
+ var args = Array.prototype.slice.call(arguments,1);
+ if(args.length <= 1) args = args[0];
+ callback.call(null, err, args || null);
+ });
+ }
+ }, callback);
+ }
+ else {
+ var results = {};
+ async.forEach(_keys(tasks), function (k, callback) {
+ tasks[k](function (err) {
var args = Array.prototype.slice.call(arguments,1);
if(args.length <= 1) args = args[0];
- callback.call(null, err, args || null);
+ results[k] = args;
+ callback(err);
});
- }
- }, callback);
+ }, function (err) {
+ callback(err, results);
+ });
+ }
};
async.series = function(tasks, callback){
callback = callback || function(){};
- async.mapSeries(tasks, function(fn, callback){
- if(fn){
- fn(function(err){
+ if (tasks.constructor === Array) {
+ async.mapSeries(tasks, function(fn, callback){
+ if(fn){
+ fn(function(err){
+ var args = Array.prototype.slice.call(arguments,1);
+ if(args.length <= 1) args = args[0];
+ callback.call(null, err, args || null);
+ });
+ }
+ }, callback);
+ }
+ else {
+ var results = {};
+ async.forEachSeries(_keys(tasks), function (k, callback) {
+ tasks[k](function (err) {
var args = Array.prototype.slice.call(arguments,1);
if(args.length <= 1) args = args[0];
- callback.call(null, err, args || null);
+ results[k] = args;
+ callback(err);
});
- }
- }, callback);
+ }, function (err) {
+ callback(err, results);
+ });
+ }
};
async.iterator = function(tasks){
diff --git a/test/test-async.js b/test/test-async.js
index e9aefd6..09df38c 100644
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -234,6 +234,40 @@ exports['parallel no callback'] = function(test){
]);
};
+exports['parallel object'] = function(test){
+ var call_order = [];
+ async.parallel({
+ one: function(callback){
+ setTimeout(function(){
+ call_order.push(1);
+ callback(null, 1);
+ }, 25);
+ },
+ two: function(callback){
+ setTimeout(function(){
+ call_order.push(2);
+ callback(null, 2);
+ }, 50);
+ },
+ three: function(callback){
+ setTimeout(function(){
+ call_order.push(3);
+ callback(null, 3,3);
+ }, 15);
+ }
+ },
+ function(err, results){
+ test.equals(err, null);
+ test.same(call_order, [3,1,2]);
+ test.same(results, {
+ one: 1,
+ two: 2,
+ three: [3,3]
+ });
+ test.done();
+ });
+};
+
exports['series'] = function(test){
var call_order = [];
async.series([
@@ -296,6 +330,40 @@ exports['series no callback'] = function(test){
]);
};
+exports['series object'] = function(test){
+ var call_order = [];
+ async.series({
+ one: function(callback){
+ setTimeout(function(){
+ call_order.push(1);
+ callback(null, 1);
+ }, 25);
+ },
+ two: function(callback){
+ setTimeout(function(){
+ call_order.push(2);
+ callback(null, 2);
+ }, 50);
+ },
+ three: function(callback){
+ setTimeout(function(){
+ call_order.push(3);
+ callback(null, 3,3);
+ }, 15);
+ }
+ },
+ function(err, results){
+ test.equals(err, null);
+ test.same(results, {
+ one: 1,
+ two: 2,
+ three: [3,3]
+ });
+ test.same(call_order, [1,2,3]);
+ test.done();
+ });
+};
+
exports['iterator'] = function(test){
var call_order = [];
var iterator = async.iterator([
--
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