[Pkg-javascript-commits] [node-async] 89/480: In Auto function, save results from functions and pass them forward to dependents.

Jonas Smedegaard js at moszumanska.debian.org
Fri May 2 08:58:15 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 c66881d411fe3797aca1b93eec17043496b568eb
Author: Allan Carroll <allanca at gmail.com>
Date:   Wed May 25 12:17:40 2011 -0700

    In Auto function, save results from functions and pass them forward to dependents.
---
 README.md          |  9 ++++++---
 dist/async.min.js  |  2 +-
 lib/async.js       | 16 ++++++++++------
 test/test-async.js | 36 ++++++++++++++++++++++++++++++++++++
 4 files changed, 53 insertions(+), 10 deletions(-)

diff --git a/README.md b/README.md
index 31f5a42..1fe84b4 100644
--- a/README.md
+++ b/README.md
@@ -711,9 +711,10 @@ __Example__
 Determines the best order for running functions based on their requirements.
 Each function can optionally depend on other functions being completed first,
 and each function is run as soon as its requirements are satisfied. If any of
-the functions pass and error to their callback, that function will not complete
+the functions pass an error to their callback, that function will not complete
 (so any other functions depending on it will not run) and the main callback
-will be called immediately with the error.
+will be called immediately with the error. Functions also receive an object
+containing the results of functions on which they depend.
 
 __Arguments__
 
@@ -737,9 +738,11 @@ __Example__
         write_file: ['get_data', 'make_folder', function(callback){
             // once there is some data and the directory exists,
             // write the data to a file in the directory
+            callback(null, filename);
         }],
-        email_link: ['write_file', function(callback){
+        email_link: ['write_file', function(callback, results){
             // once the file is written let's email a link to it...
+            // results.write_file contains the filename returned by write_file.
         }]
     });
 
diff --git a/dist/async.min.js b/dist/async.min.js
index 0332e4b..24ac718 100644
--- a/dist/async.min.js
+++ b/dist/async.min.js
@@ -1 +1 @@
-/*global setTimeout: false, console: false */(function(){var a={};var b=this,c=b.async;typeof module!=="undefined"&&module.exports?module.exports=a:b.async=a,a.noConflict=function(){b.async=c;return a};var d=function(a,b){if(a.forEach)return a.forEach(b);for(var c=0;c<a.length;c+=1)b(a[c],c,a)};var e=function(a,b){if(a.map)return a.map(b);var c=[];d(a,function(a,d,e){c.push(b(a,d,e))});return c};var f=function(a,b,c){if(a.reduce)return a.reduce(b,c);d(a,function(a,d,e){c=b(c,a,d,e)});ret [...]
\ No newline at end of file
+/*global setTimeout: false, console: false */(function(){var a={},b=this,c=b.async;typeof module!="undefined"&&module.exports?module.exports=a:b.async=a,a.noConflict=function(){b.async=c;return a};var d=function(a,b){if(a.forEach)return a.forEach(b);for(var c=0;c<a.length;c+=1)b(a[c],c,a)},e=function(a,b){if(a.map)return a.map(b);var c=[];d(a,function(a,d,e){c.push(b(a,d,e))});return c},f=function(a,b,c){if(a.reduce)return a.reduce(b,c);d(a,function(a,d,e){c=b(c,a,d,e)});return c},g=func [...]
\ No newline at end of file
diff --git a/lib/async.js b/lib/async.js
index 67de211..106b68c 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -325,7 +325,7 @@
             return callback(null);
         }
 
-        var completed = [];
+        var results = {};
 
         var listeners = [];
         var addListener = function (fn) {
@@ -346,8 +346,8 @@
         };
 
         addListener(function () {
-            if (completed.length === keys.length) {
-                callback(null);
+            if (_keys(results).length === keys.length) {
+                callback(null, results);
             }
         });
 
@@ -360,14 +360,18 @@
                     callback = function () {};
                 }
                 else {
-                    completed.push(k);
+                    var args = Array.prototype.slice.call(arguments, 1);
+                    if (args.length <= 1) {
+                        args = args[0];
+                    }
+                    results[k] = args;
                     taskComplete();
                 }
             };
             var requires = task.slice(0, Math.abs(task.length - 1)) || [];
             var ready = function () {
                 return _reduce(requires, function (a, x) {
-                    return (a && _indexOf(completed, x) !== -1);
+                    return (a && results.hasOwnProperty(x));
                 }, true);
             };
             if (ready()) {
@@ -377,7 +381,7 @@
                 var listener = function () {
                     if (ready()) {
                         removeListener(listener);
-                        task[task.length - 1](taskCallback);
+                        task[task.length - 1](taskCallback, results);
                     }
                 };
                 addListener(listener);
diff --git a/test/test-async.js b/test/test-async.js
index 4a7f986..2c57daf 100644
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -32,6 +32,42 @@ exports['auto'] = function(test){
     });
 };
 
+exports['auto results'] = function(test){
+    var callOrder = [];
+    async.auto({
+      task1: ['task2', function(callback, results){
+          test.same(results.task2, 'task2');
+          setTimeout(function(){
+              callOrder.push('task1');
+              callback(null, 'task1a', 'task1b');
+          }, 25);
+      }],
+      task2: function(callback){
+          setTimeout(function(){
+              callOrder.push('task2');
+              callback(null, 'task2');
+          }, 50);
+      },
+      task3: ['task2', function(callback, results){
+          test.same(results.task2, 'task2');
+          callOrder.push('task3');
+          callback(null);
+      }],
+      task4: ['task1', 'task2', function(callback, results){
+          test.same(results.task1, ['task1a','task1b']);
+          test.same(results.task2, 'task2');
+          callOrder.push('task4');
+          callback(null, 'task4');
+      }]
+    },
+    function(err, results){
+        test.same(callOrder, ['task2','task3','task1','task4']);
+        test.same(results, {task1: ['task1a','task1b'], task2: 'task2', task3: undefined, task4: 'task4'});
+        test.done();
+    });
+};
+
+
 exports['auto empty object'] = function(test){
     async.auto({}, function(err){
         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