summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGraeme Yeates <yeatesgraeme@gmail.com>2015-11-26 15:02:16 -0500
committerGraeme Yeates <yeatesgraeme@gmail.com>2015-11-26 15:02:16 -0500
commite7b334c20f4b4c9c7f438f93e64af6c640a0360b (patch)
treee4db499a7124f0d2d4325ff6ccbb95c622fe7fdb
parent7684e7ecd010cf3664f8fcea516011ecfc72f396 (diff)
parentf93765079edf97cb1c169d4e42d221c0e3544e8a (diff)
downloadasync-e7b334c20f4b4c9c7f438f93e64af6c640a0360b.tar.gz
Merge pull request #963 from benfleis/master
pass last result to while/until callback
-rw-r--r--README.md16
-rw-r--r--lib/async.js2
-rwxr-xr-xtest/test-async.js38
3 files changed, 33 insertions, 23 deletions
diff --git a/README.md b/README.md
index 1d8f535..2a80bf1 100644
--- a/README.md
+++ b/README.md
@@ -834,8 +834,9 @@ __Arguments__
* `fn(callback)` - A function which is called each time `test` passes. The function is
passed a `callback(err)`, which must be called once it has completed with an
optional `err` argument.
-* `callback(err)` - A callback which is called after the test fails and repeated
- execution of `fn` has stopped.
+* `callback(err, [results])` - A callback which is called after the test
+ function has failed and repeated execution of `fn` has stopped. `callback`
+ will be passed an error and any arguments passed to the final `fn`'s callback.
__Example__
@@ -846,10 +847,12 @@ async.whilst(
function () { return count < 5; },
function (callback) {
count++;
- setTimeout(callback, 1000);
+ setTimeout(function () {
+ callback(null, count);
+ }, 1000);
},
- function (err) {
- // 5 seconds have passed
+ function (err, n) {
+ // 5 seconds have passed, n = 5
}
);
```
@@ -870,7 +873,8 @@ the order of operations, the arguments `test` and `fn` are switched.
### until(test, fn, callback)
Repeatedly call `fn` until `test` returns `true`. Calls `callback` when stopped,
-or an error occurs.
+or an error occurs. `callback` will be passed an error and any arguments passed
+to the final `fn`'s callback.
The inverse of [`whilst`](#whilst).
diff --git a/lib/async.js b/lib/async.js
index 5c17819..08af693 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -781,7 +781,7 @@
} else if (test.apply(this, args)) {
iterator(next);
} else {
- callback(null);
+ callback.apply(null, [null].concat(args));
}
});
iterator(next);
diff --git a/test/test-async.js b/test/test-async.js
index 5777744..6cce049 100755
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -2812,7 +2812,7 @@ exports['concatSeries'] = function(test){
};
exports['until'] = function (test) {
- test.expect(3);
+ test.expect(4);
var call_order = [];
var count = 0;
@@ -2824,10 +2824,11 @@ exports['until'] = function (test) {
function (cb) {
call_order.push(['iterator', count]);
count++;
- cb();
+ cb(null, count);
},
- function (err) {
+ function (err, result) {
test.ok(err === null, err + " passed instead of 'null'");
+ test.equals(result, 5, 'last result passed through');
test.same(call_order, [
['test', 0],
['iterator', 0], ['test', 1],
@@ -2843,7 +2844,7 @@ exports['until'] = function (test) {
};
exports['doUntil'] = function (test) {
- test.expect(3);
+ test.expect(4);
var call_order = [];
var count = 0;
@@ -2851,14 +2852,15 @@ exports['doUntil'] = function (test) {
function (cb) {
call_order.push(['iterator', count]);
count++;
- cb();
+ cb(null, count);
},
function () {
call_order.push(['test', count]);
return (count == 5);
},
- function (err) {
+ function (err, result) {
test.ok(err === null, err + " passed instead of 'null'");
+ test.equals(result, 5, 'last result passed through');
test.same(call_order, [
['iterator', 0], ['test', 1],
['iterator', 1], ['test', 2],
@@ -2873,7 +2875,7 @@ exports['doUntil'] = function (test) {
};
exports['doUntil callback params'] = function (test) {
- test.expect(2);
+ test.expect(3);
var call_order = [];
var count = 0;
@@ -2887,8 +2889,9 @@ exports['doUntil callback params'] = function (test) {
call_order.push(['test', c]);
return (c == 5);
},
- function (err) {
+ function (err, result) {
if (err) throw err;
+ test.equals(result, 5, 'last result passed through');
test.same(call_order, [
['iterator', 0], ['test', 1],
['iterator', 1], ['test', 2],
@@ -2903,7 +2906,7 @@ exports['doUntil callback params'] = function (test) {
};
exports['whilst'] = function (test) {
- test.expect(3);
+ test.expect(4);
var call_order = [];
@@ -2916,10 +2919,11 @@ exports['whilst'] = function (test) {
function (cb) {
call_order.push(['iterator', count]);
count++;
- cb();
+ cb(null, count);
},
- function (err) {
+ function (err, result) {
test.ok(err === null, err + " passed instead of 'null'");
+ test.equals(result, 5, 'last result passed through');
test.same(call_order, [
['test', 0],
['iterator', 0], ['test', 1],
@@ -2935,7 +2939,7 @@ exports['whilst'] = function (test) {
};
exports['doWhilst'] = function (test) {
- test.expect(3);
+ test.expect(4);
var call_order = [];
var count = 0;
@@ -2943,14 +2947,15 @@ exports['doWhilst'] = function (test) {
function (cb) {
call_order.push(['iterator', count]);
count++;
- cb();
+ cb(null, count);
},
function () {
call_order.push(['test', count]);
return (count < 5);
},
- function (err) {
+ function (err, result) {
test.ok(err === null, err + " passed instead of 'null'");
+ test.equals(result, 5, 'last result passed through');
test.same(call_order, [
['iterator', 0], ['test', 1],
['iterator', 1], ['test', 2],
@@ -2965,7 +2970,7 @@ exports['doWhilst'] = function (test) {
};
exports['doWhilst callback params'] = function (test) {
- test.expect(2);
+ test.expect(3);
var call_order = [];
var count = 0;
async.doWhilst(
@@ -2978,8 +2983,9 @@ exports['doWhilst callback params'] = function (test) {
call_order.push(['test', c]);
return (c < 5);
},
- function (err) {
+ function (err, result) {
if (err) throw err;
+ test.equals(result, 5, 'last result passed through');
test.same(call_order, [
['iterator', 0], ['test', 1],
['iterator', 1], ['test', 2],