diff options
author | Alexander Early <alexander.early@gmail.com> | 2015-06-28 15:36:15 -0700 |
---|---|---|
committer | Alexander Early <alexander.early@gmail.com> | 2015-06-28 15:36:15 -0700 |
commit | b58f9582430dbab758b7d81a489200dc30200aa4 (patch) | |
tree | 6eb31f69694379bf5d351e141909e675ceeee0ac | |
parent | e7948013f3c3741606159e0c86f03dbb9e20ff2b (diff) | |
parent | 4ec75498d18b72893b20a5b617ec817d7bb8da17 (diff) | |
download | async-b58f9582430dbab758b7d81a489200dc30200aa4.tar.gz |
Merge pull request #800 from uzyn/during
during & doDuring with async test
-rw-r--r-- | README.md | 38 | ||||
-rw-r--r-- | lib/async.js | 40 | ||||
-rwxr-xr-x | test/test-async.js | 59 |
3 files changed, 137 insertions, 0 deletions
@@ -192,6 +192,8 @@ Usage: * [`doWhilst`](#doWhilst) * [`until`](#until) * [`doUntil`](#doUntil) +* [`during`](#during) +* [`doDuring`](#doDuring) * [`forever`](#forever) * [`waterfall`](#waterfall) * [`compose`](#compose) @@ -991,6 +993,42 @@ Like [`doWhilst`](#doWhilst), except the `test` is inverted. Note the argument o --------------------------------------- +<a name="during" /> +### during(test, fn, callback) + +Like [`whilst`](#whilst), except the `test` is an asynchronous function that is passed a callback in the form of `function (err, truth)`. If error is passed to `test` or `fn`, the main callback is immediately called with the value of the error. + +__Example__ + +```js +var count = 0; + +async.during( + function (callback) { + return callback(null, count < 5); + }, + function (callback) { + count++; + setTimeout(callback, 1000); + }, + function (err) { + // 5 seconds have passed + } +); +``` + +--------------------------------------- + +<a name="doDuring" /> +### doDuring(fn, test, callback) + +The post-check version of [`during`](#during). To reflect the difference in +the order of operations, the arguments `test` and `fn` are switched. + +Also a version of [`doWhilst`](#doWhilst) with asynchronous `test` function. + +--------------------------------------- + <a name="forever" /> ### forever(fn, [errback]) diff --git a/lib/async.js b/lib/async.js index 6191b57..7154ca2 100644 --- a/lib/async.js +++ b/lib/async.js @@ -846,6 +846,46 @@ }); }; + async.during = function (test, iterator, callback) { + test(function(err, truth) { + if (err) { + return callback(err); + } + 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 (err, truth) { + if (err) { + return callback(err); + } + 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 321694a..25e964e 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -2847,6 +2847,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(null, 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(null, 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['whilst optional callback'] = function (test) { var counter = 0; async.whilst( |