diff options
author | U-Zyn Chua <chua@uzyn.com> | 2015-06-23 22:25:02 +0800 |
---|---|---|
committer | U-Zyn Chua <chua@uzyn.com> | 2015-06-23 22:25:02 +0800 |
commit | ea3576b6da334710d0da7446064e9acc0766136a (patch) | |
tree | 9783f8c78f4019b5869e2051de27b71259b81347 | |
parent | bd8325fb5b07b296733f1638fa8dcd7b8fc684cd (diff) | |
download | async-ea3576b6da334710d0da7446064e9acc0766136a.tar.gz |
during & doDuring with async test
-rw-r--r-- | lib/async.js | 34 | ||||
-rwxr-xr-x | test/test-async.js | 59 |
2 files changed, 93 insertions, 0 deletions
diff --git a/lib/async.js b/lib/async.js index de50c76..6cbe301 100644 --- a/lib/async.js +++ b/lib/async.js @@ -858,6 +858,40 @@ }); }; + async.during = function (test, iterator, callback) { + test(function(truth) { + if (truth) { + iterator(function (err) { + if (err) { + return callback(err); + } + async.during(test, iterator, callback); + }); + } + else { + callback(null); + } + }); + }; + + async.doDuring = function (iterator, test, callback) { + iterator(function (err) { + if (err) { + return callback(err); + } + var args = _baseSlice(arguments, 1); + args.push(function (truth) { + if (truth) { + async.doDuring(iterator, test, callback); + } + else { + callback(null); + } + }); + test.apply(null, args); + }); + }; + function _queue(worker, concurrency, payload) { if (concurrency == null) { concurrency = 1; diff --git a/test/test-async.js b/test/test-async.js index 8b2f87f..2d7269b 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -2811,6 +2811,65 @@ exports['doWhilst callback params'] = function (test) { ); }; +exports['during'] = function (test) { + var call_order = []; + + var count = 0; + async.during( + function (cb) { + call_order.push(['test', count]); + cb(count < 5); + }, + function (cb) { + call_order.push(['iterator', count]); + count++; + cb(); + }, + function (err) { + test.ok(err === null, err + " passed instead of 'null'"); + 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['doDuring'] = function (test) { + var call_order = []; + + var count = 0; + async.doDuring( + function (cb) { + call_order.push(['iterator', count]); + count++; + cb(); + }, + function (cb) { + call_order.push(['test', count]); + cb(count < 5); + }, + function (err) { + test.ok(err === null, err + " passed instead of 'null'"); + test.same(call_order, [ + ['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['queue'] = { 'queue': function (test) { |