Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

added doWhilst and doUntil #204

Merged
merged 2 commits into from about 1 year ago

4 participants

Jake Rosoman Farid Nouri Neshat Bulkan Evcimen Caolan McMahon
Jake Rosoman

Sometimes post check loops are handy. Today I had one of those moments.

Farid Nouri Neshat

Documentation need to be added.

Caolan McMahon caolan merged commit 0331124 into from January 31, 2013
Caolan McMahon caolan closed this January 31, 2013
Bulkan Evcimen

Is this meant to be here ? :)

Owner

Oh, just realised these have been added ages ago and whatever debugger statements have been removed already.

Caolan McMahon

this debugger statement will also need removing

Side note, there is also duplicate tests both with the same name times not sure if its intentional

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Nov 29, 2012
Jake Rosoman added doWhilst and doUntil fd41664
Jake Rosoman added post loop docs 0331124
This page is out of date. Refresh to see the latest.
16  README.md
Source Rendered
@@ -88,7 +88,9 @@ So far its been tested in IE6, IE7, IE8, FF3.6 and Chrome 5. Usage:
88 88
 * [series](#series)
89 89
 * [parallel](#parallel)
90 90
 * [whilst](#whilst)
  91
+* [doWhilst](#doWhilst)
91 92
 * [until](#until)
  93
+* [doUntil](#doUntil)
92 94
 * [waterfall](#waterfall)
93 95
 * [queue](#queue)
94 96
 * [auto](#auto)
@@ -664,6 +666,13 @@ async.whilst(
664 666
 
665 667
 ---------------------------------------
666 668
 
  669
+<a name="doWhilst" />
  670
+### doWhilst(fn, test, callback)
  671
+
  672
+The post check version of whilst. To reflect the difference in the order of operations `test` and `fn` arguments are switched. `doWhilst` is to `whilst` as `do while` is to `while` in plain JavaScript.
  673
+
  674
+---------------------------------------
  675
+
667 676
 <a name="until" />
668 677
 ### until(test, fn, callback)
669 678
 
@@ -672,6 +681,13 @@ or an error occurs.
672 681
 
673 682
 The inverse of async.whilst.
674 683
 
  684
+---------------------------------------
  685
+
  686
+<a name="doUntil" />
  687
+### doUntil(fn, test, callback)
  688
+
  689
+Like doWhilst except the test is inverted. Note the argument ordering differs from `until`.
  690
+
675 691
 
676 692
 ---------------------------------------
677 693
 
1  dist/async.min.js
... ...
@@ -1 +0,0 @@
1  
-/*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(){return b.async=c,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=[];return d(a,function(a,d,e){c.push(b(a,d,e))}),c},f=function(a,b,c){return a.reduce?a.reduce(b,c):(d(a,function(a,d,e){c=b(c,a,d,e)}),c)},g=function(a){if(Object.keys)return Object.keys(a);var b=[];for(var c in a)a.hasOwnProperty(c)&&b.push(c);return b};typeof process=="undefined"||!process.nextTick?a.nextTick=function(a){setTimeout(a,0)}:a.nextTick=process.nextTick,a.forEach=function(a,b,c){c=c||function(){};if(!a.length)return c();var e=0;d(a,function(d){b(d,function(b){b?(c(b),c=function(){}):(e+=1,e===a.length&&c(null))})})},a.forEachSeries=function(a,b,c){c=c||function(){};if(!a.length)return c();var d=0,e=function(){b(a[d],function(b){b?(c(b),c=function(){}):(d+=1,d===a.length?c(null):e())})};e()},a.forEachLimit=function(a,b,c,d){d=d||function(){};if(!a.length||b<=0)return d();var e=0,f=0,g=0;(function h(){if(e===a.length)return d();while(g<b&&f<a.length)f+=1,g+=1,c(a[f-1],function(b){b?(d(b),d=function(){}):(e+=1,g-=1,e===a.length?d():h())})})()};var h=function(b){return function(){var c=Array.prototype.slice.call(arguments);return b.apply(null,[a.forEach].concat(c))}},i=function(b){return function(){var c=Array.prototype.slice.call(arguments);return b.apply(null,[a.forEachSeries].concat(c))}},j=function(a,b,c,d){var f=[];b=e(b,function(a,b){return{index:b,value:a}}),a(b,function(a,b){c(a.value,function(c,d){f[a.index]=d,b(c)})},function(a){d(a,f)})};a.map=h(j),a.mapSeries=i(j),a.reduce=function(b,c,d,e){a.forEachSeries(b,function(a,b){d(c,a,function(a,d){c=d,b(a)})},function(a){e(a,c)})},a.inject=a.reduce,a.foldl=a.reduce,a.reduceRight=function(b,c,d,f){var g=e(b,function(a){return a}).reverse();a.reduce(g,c,d,f)},a.foldr=a.reduceRight;var k=function(a,b,c,d){var f=[];b=e(b,function(a,b){return{index:b,value:a}}),a(b,function(a,b){c(a.value,function(c){c&&f.push(a),b()})},function(a){d(e(f.sort(function(a,b){return a.index-b.index}),function(a){return a.value}))})};a.filter=h(k),a.filterSeries=i(k),a.select=a.filter,a.selectSeries=a.filterSeries;var l=function(a,b,c,d){var f=[];b=e(b,function(a,b){return{index:b,value:a}}),a(b,function(a,b){c(a.value,function(c){c||f.push(a),b()})},function(a){d(e(f.sort(function(a,b){return a.index-b.index}),function(a){return a.value}))})};a.reject=h(l),a.rejectSeries=i(l);var m=function(a,b,c,d){a(b,function(a,b){c(a,function(c){c?(d(a),d=function(){}):b()})},function(a){d()})};a.detect=h(m),a.detectSeries=i(m),a.some=function(b,c,d){a.forEach(b,function(a,b){c(a,function(a){a&&(d(!0),d=function(){}),b()})},function(a){d(!1)})},a.any=a.some,a.every=function(b,c,d){a.forEach(b,function(a,b){c(a,function(a){a||(d(!1),d=function(){}),b()})},function(a){d(!0)})},a.all=a.every,a.sortBy=function(b,c,d){a.map(b,function(a,b){c(a,function(c,d){c?b(c):b(null,{value:a,criteria:d})})},function(a,b){if(a)return d(a);var c=function(a,b){var c=a.criteria,d=b.criteria;return c<d?-1:c>d?1:0};d(null,e(b.sort(c),function(a){return a.value}))})},a.auto=function(a,b){b=b||function(){};var c=g(a);if(!c.length)return b(null);var e={},h=[],i=function(a){h.unshift(a)},j=function(a){for(var b=0;b<h.length;b+=1)if(h[b]===a){h.splice(b,1);return}},k=function(){d(h.slice(0),function(a){a()})};i(function(){g(e).length===c.length&&(b(null,e),b=function(){})}),d(c,function(c){var d=a[c]instanceof Function?[a[c]]:a[c],g=function(a){if(a)b(a),b=function(){};else{var d=Array.prototype.slice.call(arguments,1);d.length<=1&&(d=d[0]),e[c]=d,k()}},h=d.slice(0,Math.abs(d.length-1))||[],l=function(){return f(h,function(a,b){return a&&e.hasOwnProperty(b)},!0)&&!e.hasOwnProperty(c)};if(l())d[d.length-1](g,e);else{var m=function(){l()&&(j(m),d[d.length-1](g,e))};i(m)}})},a.waterfall=function(b,c){c=c||function(){};if(!b.length)return c();var d=function(b){return function(e){if(e)c(e),c=function(){};else{var f=Array.prototype.slice.call(arguments,1),g=b.next();g?f.push(d(g)):f.push(c),a.nextTick(function(){b.apply(null,f)})}}};d(a.iterator(b))()},a.parallel=function(b,c){c=c||function(){};if(b.constructor===Array)a.map(b,function(a,b){a&&a(function(a){var c=Array.prototype.slice.call(arguments,1);c.length<=1&&(c=c[0]),b.call(null,a,c)})},c);else{var d={};a.forEach(g(b),function(a,c){b[a](function(b){var e=Array.prototype.slice.call(arguments,1);e.length<=1&&(e=e[0]),d[a]=e,c(b)})},function(a){c(a,d)})}},a.series=function(b,c){c=c||function(){};if(b.constructor===Array)a.mapSeries(b,function(a,b){a&&a(function(a){var c=Array.prototype.slice.call(arguments,1);c.length<=1&&(c=c[0]),b.call(null,a,c)})},c);else{var d={};a.forEachSeries(g(b),function(a,c){b[a](function(b){var e=Array.prototype.slice.call(arguments,1);e.length<=1&&(e=e[0]),d[a]=e,c(b)})},function(a){c(a,d)})}},a.iterator=function(a){var b=function(c){var d=function(){return a.length&&a[c].apply(null,arguments),d.next()};return d.next=function(){return c<a.length-1?b(c+1):null},d};return b(0)},a.apply=function(a){var b=Array.prototype.slice.call(arguments,1);return function(){return a.apply(null,b.concat(Array.prototype.slice.call(arguments)))}};var n=function(a,b,c,d){var e=[];a(b,function(a,b){c(a,function(a,c){e=e.concat(c||[]),b(a)})},function(a){d(a,e)})};a.concat=h(n),a.concatSeries=i(n),a.whilst=function(b,c,d){b()?c(function(e){if(e)return d(e);a.whilst(b,c,d)}):d()},a.until=function(b,c,d){b()?d():c(function(e){if(e)return d(e);a.until(b,c,d)})},a.queue=function(b,c){var e=0,f={tasks:[],concurrency:c,saturated:null,empty:null,drain:null,push:function(b,e){b.constructor!==Array&&(b=[b]),d(b,function(b){f.tasks.push({data:b,callback:typeof e=="function"?e:null}),f.saturated&&f.tasks.length==c&&f.saturated(),a.nextTick(f.process)})},process:function(){if(e<f.concurrency&&f.tasks.length){var a=f.tasks.shift();f.empty&&f.tasks.length==0&&f.empty(),e+=1,b(a.data,function(){e-=1,a.callback&&a.callback.apply(a,arguments),f.drain&&f.tasks.length+e==0&&f.drain(),f.process()})}},length:function(){return f.tasks.length},running:function(){return e}};return f};var o=function(a){return function(b){var c=Array.prototype.slice.call(arguments,1);b.apply(null,c.concat([function(b){var c=Array.prototype.slice.call(arguments,1);typeof console!="undefined"&&(b?console.error&&console.error(b):console[a]&&d(c,function(b){console[a](b)}))}]))}};a.log=o("log"),a.dir=o("dir"),a.memoize=function(a,b){var c={},d={};b=b||function(a){return a};var e=function(){var e=Array.prototype.slice.call(arguments),f=e.pop(),g=b.apply(null,e);g in c?f.apply(null,c[g]):g in d?d[g].push(f):(d[g]=[f],a.apply(null,e.concat([function(){c[g]=arguments;var a=d[g];delete d[g];for(var b=0,e=a.length;b<e;b++)a[b].apply(null,arguments)}])))};return e.unmemoized=a,e},a.unmemoize=function(a){return function(){return(a.unmemoized||a).apply(null,arguments)}}})();
28  lib/async.js
@@ -556,6 +556,20 @@
556 556
         }
557 557
     };
558 558
 
  559
+    async.doWhilst = function (iterator, test, callback) {
  560
+        iterator(function (err) {
  561
+            if (err) {
  562
+                return callback(err);
  563
+            }
  564
+            if (test()) {
  565
+                async.doWhilst(iterator, test, callback);
  566
+            }
  567
+            else {
  568
+                callback();
  569
+            }
  570
+        });
  571
+    };
  572
+
559 573
     async.until = function (test, iterator, callback) {
560 574
         if (!test()) {
561 575
             iterator(function (err) {
@@ -570,6 +584,20 @@
570 584
         }
571 585
     };
572 586
 
  587
+    async.doUntil = function (iterator, test, callback) {
  588
+        iterator(function (err) {
  589
+            if (err) {
  590
+                return callback(err);
  591
+            }
  592
+            if (!test()) {
  593
+                async.doUntil(iterator, test, callback);
  594
+            }
  595
+            else {
  596
+                callback();
  597
+            }
  598
+        });
  599
+    };
  600
+
573 601
     async.queue = function (worker, concurrency) {
574 602
         var workers = 0;
575 603
         var q = {
57  test/test-async.js
@@ -1213,6 +1213,34 @@ exports['until'] = function (test) {
1213 1213
     );
1214 1214
 };
1215 1215
 
  1216
+exports['doUntil'] = function (test) {
  1217
+    var call_order = [];
  1218
+    var count = 0;
  1219
+    async.doUntil(
  1220
+        function (cb) {
  1221
+            debugger
  1222
+            call_order.push(['iterator', count]);
  1223
+            count++;
  1224
+            cb();
  1225
+        },
  1226
+        function () {
  1227
+            call_order.push(['test', count]);
  1228
+            return (count == 5);
  1229
+        },
  1230
+        function (err) {
  1231
+            test.same(call_order, [
  1232
+                ['iterator', 0], ['test', 1],
  1233
+                ['iterator', 1], ['test', 2],
  1234
+                ['iterator', 2], ['test', 3],
  1235
+                ['iterator', 3], ['test', 4],
  1236
+                ['iterator', 4], ['test', 5]
  1237
+            ]);
  1238
+            test.equals(count, 5);
  1239
+            test.done();
  1240
+        }
  1241
+    );
  1242
+};
  1243
+
1216 1244
 exports['whilst'] = function (test) {
1217 1245
     var call_order = [];
1218 1246
 
@@ -1242,6 +1270,35 @@ exports['whilst'] = function (test) {
1242 1270
     );
1243 1271
 };
1244 1272
 
  1273
+exports['doWhilst'] = function (test) {
  1274
+    var call_order = [];
  1275
+
  1276
+    var count = 0;
  1277
+    async.doWhilst(
  1278
+        function (cb) {
  1279
+            call_order.push(['iterator', count]);
  1280
+            count++;
  1281
+            cb();
  1282
+        },
  1283
+        function () {
  1284
+            call_order.push(['test', count]);
  1285
+            return (count < 5);
  1286
+        },
  1287
+        function (err) {
  1288
+            debugger
  1289
+            test.same(call_order, [
  1290
+                ['iterator', 0], ['test', 1],
  1291
+                ['iterator', 1], ['test', 2],
  1292
+                ['iterator', 2], ['test', 3],
  1293
+                ['iterator', 3], ['test', 4],
  1294
+                ['iterator', 4], ['test', 5]
  1295
+            ]);
  1296
+            test.equals(count, 5);
  1297
+            test.done();
  1298
+        }
  1299
+    );
  1300
+};
  1301
+
1245 1302
 exports['queue'] = function (test) {
1246 1303
     var call_order = [],
1247 1304
         delays = [160,80,240,80];
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.