[Pkg-javascript-commits] [node-async] 43/480: add whilst and until

Jonas Smedegaard js at moszumanska.debian.org
Fri May 2 08:58:10 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 b53b337c7041a89b83332fd623a0d2b11a15a664
Author: Caolan McMahon <caolan at caolanmcmahon.com>
Date:   Fri Oct 15 18:04:30 2010 +0100

    add whilst and until
---
 README.md          | 41 ++++++++++++++++++++++++++++++++++++++
 lib/async.js       | 20 +++++++++++++++++++
 test/test-async.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 119 insertions(+)

diff --git a/README.md b/README.md
index 852a64e..485227d 100644
--- a/README.md
+++ b/README.md
@@ -103,6 +103,11 @@ concat
 : Apply an iterator to each item in a list and concatenate the results. Series
   version: concatSeries.
 
+whilst
+: Repeatedly call an async function while a test function returns true.
+
+until
+: Repeatedly call an async function until a test function returns true.
 
 ### Flow Control
 
@@ -435,6 +440,42 @@ __Example__
 
 Same as async.concat, but executes in series instead of parallel.
 
+### whilst(test, fn, callback)
+
+Repeatedly call fn, while test returns true. Calls the callback when stopped,
+or an error occurs.
+
+__Arguments__
+
+* test() - synchronous truth test to perform before each execution of fn.
+* fn(callback) - A function to call each time the test passes. The function is
+  passed a callback which must be called once it has completed with an optional
+  error as the first argument.
+* callback(err) - A callback which is called after the test fails and repeated
+  execution of fn has stopped.
+
+__Example__
+
+    var count = 0;
+
+    async.whilst(
+        function () { return count < 5; },
+        function (callback) {
+            count++;
+            setTimeout(callback, 1000);
+        },
+        function (err) {
+            // 5 seconds have passed
+        }
+    });
+
+### until(test, fn, callback)
+
+Repeatedly call fn, until test returns true. Calls the callback when stopped,
+or an error occurs.
+
+The inverse of async.whilst.
+
 
 ### series(tasks, [callback])
 
diff --git a/lib/async.js b/lib/async.js
index 9ab14f9..e557085 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -410,6 +410,26 @@
     async.concat = doParallel(_concat);
     async.concatSeries = doSeries(_concat);
 
+    async.whilst = function(test, iterator, callback){
+        if (test()) {
+            iterator(function (err) {
+                if (err) return callback(err);
+                async.whilst(test, iterator, callback);
+            });
+        }
+        else callback();
+    };
+
+    async.until = function(test, iterator, callback){
+        if (!test()) {
+            iterator(function (err) {
+                if (err) return callback(err);
+                async.until(test, iterator, callback);
+            });
+        }
+        else callback();
+    };
+
     var _console_fn = function(name){
         return function(fn){
             var args = Array.prototype.slice.call(arguments, 1);
diff --git a/test/test-async.js b/test/test-async.js
index 6f0f7b0..e9aefd6 100644
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -924,3 +924,61 @@ exports['concatSeries'] = function(test){
         test.done();
     });
 };
+
+exports['until'] = function (test) {
+    var call_order = [];
+
+    var count = 0;
+    async.until(
+        function () {
+            call_order.push(['test', count]);
+            return (count == 5);
+        },
+        function (cb) {
+            call_order.push(['iterator', count]);
+            count++;
+            cb();
+        },
+        function (err) {
+            test.same(call_order, [
+                ['test', 0],
+                ['iterator', 0], ['test', 1],
+                ['iterator', 1], ['test', 2],
+                ['iterator', 2], ['test', 3],
+                ['iterator', 3], ['test', 4],
+                ['iterator', 4], ['test', 5],
+            ]);
+            test.equals(count, 5);
+            test.done();
+        }
+    );
+};
+
+exports['whilst'] = function (test) {
+    var call_order = [];
+
+    var count = 0;
+    async.whilst(
+        function () {
+            call_order.push(['test', count]);
+            return (count < 5);
+        },
+        function (cb) {
+            call_order.push(['iterator', count]);
+            count++;
+            cb();
+        },
+        function (err) {
+            test.same(call_order, [
+                ['test', 0],
+                ['iterator', 0], ['test', 1],
+                ['iterator', 1], ['test', 2],
+                ['iterator', 2], ['test', 3],
+                ['iterator', 3], ['test', 4],
+                ['iterator', 4], ['test', 5],
+            ]);
+            test.equals(count, 5);
+            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