Skip to content

Commit e7b334c

Browse files
committed
Merge pull request #963 from benfleis/master
pass last result to while/until callback
2 parents 7684e7e + f937650 commit e7b334c

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -834,8 +834,9 @@ __Arguments__
834834
* `fn(callback)` - A function which is called each time `test` passes. The function is
835835
passed a `callback(err)`, which must be called once it has completed with an
836836
optional `err` argument.
837-
* `callback(err)` - A callback which is called after the test fails and repeated
838-
execution of `fn` has stopped.
837+
* `callback(err, [results])` - A callback which is called after the test
838+
function has failed and repeated execution of `fn` has stopped. `callback`
839+
will be passed an error and any arguments passed to the final `fn`'s callback.
839840
840841
__Example__
841842
@@ -846,10 +847,12 @@ async.whilst(
846847
function () { return count < 5; },
847848
function (callback) {
848849
count++;
849-
setTimeout(callback, 1000);
850+
setTimeout(function () {
851+
callback(null, count);
852+
}, 1000);
850853
},
851-
function (err) {
852-
// 5 seconds have passed
854+
function (err, n) {
855+
// 5 seconds have passed, n = 5
853856
}
854857
);
855858
```
@@ -870,7 +873,8 @@ the order of operations, the arguments `test` and `fn` are switched.
870873
### until(test, fn, callback)
871874
872875
Repeatedly call `fn` until `test` returns `true`. Calls `callback` when stopped,
873-
or an error occurs.
876+
or an error occurs. `callback` will be passed an error and any arguments passed
877+
to the final `fn`'s callback.
874878
875879
The inverse of [`whilst`](#whilst).
876880

lib/async.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@
781781
} else if (test.apply(this, args)) {
782782
iterator(next);
783783
} else {
784-
callback(null);
784+
callback.apply(null, [null].concat(args));
785785
}
786786
});
787787
iterator(next);

test/test-async.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2812,7 +2812,7 @@ exports['concatSeries'] = function(test){
28122812
};
28132813

28142814
exports['until'] = function (test) {
2815-
test.expect(3);
2815+
test.expect(4);
28162816

28172817
var call_order = [];
28182818
var count = 0;
@@ -2824,10 +2824,11 @@ exports['until'] = function (test) {
28242824
function (cb) {
28252825
call_order.push(['iterator', count]);
28262826
count++;
2827-
cb();
2827+
cb(null, count);
28282828
},
2829-
function (err) {
2829+
function (err, result) {
28302830
test.ok(err === null, err + " passed instead of 'null'");
2831+
test.equals(result, 5, 'last result passed through');
28312832
test.same(call_order, [
28322833
['test', 0],
28332834
['iterator', 0], ['test', 1],
@@ -2843,22 +2844,23 @@ exports['until'] = function (test) {
28432844
};
28442845

28452846
exports['doUntil'] = function (test) {
2846-
test.expect(3);
2847+
test.expect(4);
28472848

28482849
var call_order = [];
28492850
var count = 0;
28502851
async.doUntil(
28512852
function (cb) {
28522853
call_order.push(['iterator', count]);
28532854
count++;
2854-
cb();
2855+
cb(null, count);
28552856
},
28562857
function () {
28572858
call_order.push(['test', count]);
28582859
return (count == 5);
28592860
},
2860-
function (err) {
2861+
function (err, result) {
28612862
test.ok(err === null, err + " passed instead of 'null'");
2863+
test.equals(result, 5, 'last result passed through');
28622864
test.same(call_order, [
28632865
['iterator', 0], ['test', 1],
28642866
['iterator', 1], ['test', 2],
@@ -2873,7 +2875,7 @@ exports['doUntil'] = function (test) {
28732875
};
28742876

28752877
exports['doUntil callback params'] = function (test) {
2876-
test.expect(2);
2878+
test.expect(3);
28772879

28782880
var call_order = [];
28792881
var count = 0;
@@ -2887,8 +2889,9 @@ exports['doUntil callback params'] = function (test) {
28872889
call_order.push(['test', c]);
28882890
return (c == 5);
28892891
},
2890-
function (err) {
2892+
function (err, result) {
28912893
if (err) throw err;
2894+
test.equals(result, 5, 'last result passed through');
28922895
test.same(call_order, [
28932896
['iterator', 0], ['test', 1],
28942897
['iterator', 1], ['test', 2],
@@ -2903,7 +2906,7 @@ exports['doUntil callback params'] = function (test) {
29032906
};
29042907

29052908
exports['whilst'] = function (test) {
2906-
test.expect(3);
2909+
test.expect(4);
29072910

29082911
var call_order = [];
29092912

@@ -2916,10 +2919,11 @@ exports['whilst'] = function (test) {
29162919
function (cb) {
29172920
call_order.push(['iterator', count]);
29182921
count++;
2919-
cb();
2922+
cb(null, count);
29202923
},
2921-
function (err) {
2924+
function (err, result) {
29222925
test.ok(err === null, err + " passed instead of 'null'");
2926+
test.equals(result, 5, 'last result passed through');
29232927
test.same(call_order, [
29242928
['test', 0],
29252929
['iterator', 0], ['test', 1],
@@ -2935,22 +2939,23 @@ exports['whilst'] = function (test) {
29352939
};
29362940

29372941
exports['doWhilst'] = function (test) {
2938-
test.expect(3);
2942+
test.expect(4);
29392943
var call_order = [];
29402944

29412945
var count = 0;
29422946
async.doWhilst(
29432947
function (cb) {
29442948
call_order.push(['iterator', count]);
29452949
count++;
2946-
cb();
2950+
cb(null, count);
29472951
},
29482952
function () {
29492953
call_order.push(['test', count]);
29502954
return (count < 5);
29512955
},
2952-
function (err) {
2956+
function (err, result) {
29532957
test.ok(err === null, err + " passed instead of 'null'");
2958+
test.equals(result, 5, 'last result passed through');
29542959
test.same(call_order, [
29552960
['iterator', 0], ['test', 1],
29562961
['iterator', 1], ['test', 2],
@@ -2965,7 +2970,7 @@ exports['doWhilst'] = function (test) {
29652970
};
29662971

29672972
exports['doWhilst callback params'] = function (test) {
2968-
test.expect(2);
2973+
test.expect(3);
29692974
var call_order = [];
29702975
var count = 0;
29712976
async.doWhilst(
@@ -2978,8 +2983,9 @@ exports['doWhilst callback params'] = function (test) {
29782983
call_order.push(['test', c]);
29792984
return (c < 5);
29802985
},
2981-
function (err) {
2986+
function (err, result) {
29822987
if (err) throw err;
2988+
test.equals(result, 5, 'last result passed through');
29832989
test.same(call_order, [
29842990
['iterator', 0], ['test', 1],
29852991
['iterator', 1], ['test', 2],

0 commit comments

Comments
 (0)