Skip to content

Commit

Permalink
Merge pull request #800 from uzyn/during
Browse files Browse the repository at this point in the history
during & doDuring with async test
  • Loading branch information
aearly committed Jun 28, 2015
2 parents e794801 + 4ec7549 commit b58f958
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 0 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ Usage:
* [`doWhilst`](#doWhilst)
* [`until`](#until)
* [`doUntil`](#doUntil)
* [`during`](#during)
* [`doDuring`](#doDuring)
* [`forever`](#forever)
* [`waterfall`](#waterfall)
* [`compose`](#compose)
Expand Down Expand Up @@ -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])
Expand Down
40 changes: 40 additions & 0 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
59 changes: 59 additions & 0 deletions test/test-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit b58f958

Please sign in to comment.