Skip to content

Commit 569f928

Browse files
author
charlierudolph
committed
updates
1 parent e6002e8 commit 569f928

File tree

3 files changed

+47
-48
lines changed

3 files changed

+47
-48
lines changed

README.md

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async.map(['file1','file2','file3'], fs.stat, function(err, results){
3131
// results is now an array of stats for each file
3232
});
3333

34-
async.filter(['file1','file2','file3'], fs.exists, function(results){
34+
async.filter(['file1','file2','file3'], fs.access, function(err, results){
3535
// results now equals an array of the existing files
3636
});
3737

@@ -62,12 +62,12 @@ This can also arise by accident if you callback early in certain cases:
6262
```js
6363
async.eachSeries(hugeArray, function iterator(item, callback) {
6464
if (inCache(item)) {
65-
callback(null, cache[item]); // if many items are cached, you'll overflow
65+
callback(null, cache[item]); // if many items are cached, you'll overflow
6666
} else {
6767
doSomeIO(item, callback);
6868
}
69-
}, function done() {
70-
//...
69+
}, function done() {
70+
//...
7171
});
7272
```
7373

@@ -470,25 +470,22 @@ async.mapLimit(['file1','file2','file3'], 1, fs.stat, function(err, results){
470470
__Alias:__ `select`
471471
472472
Returns a new array of all the values in `arr` which pass an async truth test.
473-
_The callback for each `iterator` call only accepts a single argument of `true` or
474-
`false`; it does not accept an error argument first!_ This is in-line with the
475-
way node libraries work with truth tests like `fs.exists`. This operation is
476-
performed in parallel, but the results array will be in the same order as the
477-
original.
473+
This operation is performed in parallel,
474+
but the results array will be in the same order as the original.
478475
479476
__Arguments__
480477
481478
* `arr` - An array to iterate over.
482479
* `iterator(item, callback)` - A truth test to apply to each item in `arr`.
483-
The `iterator` is passed a `callback(truthValue)`, which must be called with a
480+
The `iterator` is passed a `callback(err, truthValue)`, which must be called with a
484481
boolean argument once it has completed.
485-
* `callback(results)` - *Optional* A callback which is called after all the `iterator`
482+
* `callback(err, results)` - *Optional* A callback which is called after all the `iterator`
486483
functions have finished.
487484
488485
__Example__
489486
490487
```js
491-
async.filter(['file1','file2','file3'], fs.exists, function(results){
488+
async.filter(['file1','file2','file3'], fs.access, function(err, results){
492489
// results now equals an array of the existing files
493490
});
494491
```
@@ -589,17 +586,17 @@ __Arguments__
589586
590587
* `arr` - An array to iterate over.
591588
* `iterator(item, callback)` - A truth test to apply to each item in `arr`.
592-
The iterator is passed a `callback(truthValue)` which must be called with a
593-
boolean argument once it has completed. **Note: this callback does not take an error as its first argument.**
594-
* `callback(result)` - *Optional* A callback which is called as soon as any iterator returns
589+
The iterator is passed a `callback(err, truthValue)` which must be called with a
590+
boolean argument once it has completed.
591+
* `callback(err, result)` - *Optional* A callback which is called as soon as any iterator returns
595592
`true`, or after all the `iterator` functions have finished. Result will be
596593
the first item in the array that passes the truth test (iterator) or the
597-
value `undefined` if none passed. **Note: this callback does not take an error as its first argument.**
594+
value `undefined` if none passed.
598595
599596
__Example__
600597
601598
```js
602-
async.detect(['file1','file2','file3'], fs.exists, function(result){
599+
async.detect(['file1','file2','file3'], fs.access, function(result){
603600
// result now equals the first file in the list that exists
604601
});
605602
```
@@ -673,26 +670,22 @@ async.sortBy([1,9,3,5], function(x, callback){
673670
__Alias:__ `any`
674671
675672
Returns `true` if at least one element in the `arr` satisfies an async test.
676-
_The callback for each iterator call only accepts a single argument of `true` or
677-
`false`; it does not accept an error argument first!_ This is in-line with the
678-
way node libraries work with truth tests like `fs.exists`. Once any iterator
679-
call returns `true`, the main `callback` is immediately called.
673+
If any iterator call returns `true`, the main `callback` is immediately called.
680674
681675
__Arguments__
682676
683677
* `arr` - An array to iterate over.
684678
* `iterator(item, callback)` - A truth test to apply to each item in the array
685679
in parallel. The iterator is passed a `callback(truthValue)`` which must be
686680
called with a boolean argument once it has completed.
687-
* `callback(result)` - *Optional* A callback which is called as soon as any iterator returns
681+
* `callback(err, result)` - *Optional* A callback which is called as soon as any iterator returns
688682
`true`, or after all the iterator functions have finished. Result will be
689683
either `true` or `false` depending on the values of the async tests.
690684

691-
**Note: the callbacks do not take an error as their first argument.**
692685
__Example__
693686

694687
```js
695-
async.some(['file1','file2','file3'], fs.exists, function(result){
688+
async.some(['file1','file2','file3'], fs.access, function(result){
696689
// if result is true then at least one of the files exists
697690
});
698691
```
@@ -705,26 +698,22 @@ async.some(['file1','file2','file3'], fs.exists, function(result){
705698
__Alias:__ `all`
706699

707700
Returns `true` if every element in `arr` satisfies an async test.
708-
_The callback for each `iterator` call only accepts a single argument of `true` or
709-
`false`; it does not accept an error argument first!_ This is in-line with the
710-
way node libraries work with truth tests like `fs.exists`.
701+
If any iterator call returns `false`, the main `callback` is immediately called.
711702

712703
__Arguments__
713704

714705
* `arr` - An array to iterate over.
715706
* `iterator(item, callback)` - A truth test to apply to each item in the array
716-
in parallel. The iterator is passed a `callback(truthValue)` which must be
707+
in parallel. The iterator is passed a `callback(err, truthValue)` which must be
717708
called with a boolean argument once it has completed.
718-
* `callback(result)` - *Optional* A callback which is called after all the `iterator`
709+
* `callback(err, result)` - *Optional* A callback which is called after all the `iterator`
719710
functions have finished. Result will be either `true` or `false` depending on
720711
the values of the async tests.
721712

722-
**Note: the callbacks do not take an error as their first argument.**
723-
724713
__Example__
725714

726715
```js
727-
async.every(['file1','file2','file3'], fs.exists, function(result){
716+
async.every(['file1','file2','file3'], fs.access, function(err, result){
728717
// if result is true then every file exists
729718
});
730719
```
@@ -1721,9 +1710,9 @@ function sometimesAsync(arg, callback) {
17211710
}
17221711

17231712
// this has a risk of stack overflows if many results are cached in a row
1724-
async.mapSeries(args, sometimesAsync, done);
1713+
async.mapSeries(args, sometimesAsync, done);
17251714

1726-
// this will defer sometimesAsync's callback if necessary,
1715+
// this will defer sometimesAsync's callback if necessary,
17271716
// preventing stack overflows
17281717
async.mapSeries(args, async.ensureAsync(sometimesAsync), done);
17291718

lib/async.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -433,17 +433,22 @@
433433

434434
function _detect(eachfn, arr, iterator, main_callback) {
435435
eachfn(arr, function (x, index, callback) {
436-
iterator(x, function (result) {
437-
if (result) {
438-
main_callback(x);
439-
main_callback = noop;
436+
iterator(x, function (err, result) {
437+
if (err) {
438+
callback(err);
440439
}
441440
else {
442-
callback();
441+
if (result) {
442+
main_callback(null, x);
443+
main_callback = noop;
444+
}
445+
else {
446+
callback();
447+
}
443448
}
444449
});
445-
}, function () {
446-
main_callback();
450+
}, function (err) {
451+
main_callback(err);
447452
});
448453
}
449454
async.detect = doParallel(_detect);

test/test-async.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function filterIterator(x, callback) {
4040
function detectIterator(call_order, x, callback) {
4141
setTimeout(function(){
4242
call_order.push(x);
43-
callback(x == 2);
43+
callback(null, x == 2);
4444
}, x*25);
4545
}
4646

@@ -2243,8 +2243,9 @@ exports['all alias'] = function(test){
22432243

22442244
exports['detect'] = function(test){
22452245
var call_order = [];
2246-
async.detect([3,2,1], detectIterator.bind(this, call_order), function(result){
2246+
async.detect([3,2,1], detectIterator.bind(this, call_order), function(err, result){
22472247
call_order.push('callback');
2248+
test.equals(err, null);
22482249
test.equals(result, 2);
22492250
});
22502251
setTimeout(function(){
@@ -2255,8 +2256,9 @@ exports['detect'] = function(test){
22552256

22562257
exports['detect - mulitple matches'] = function(test){
22572258
var call_order = [];
2258-
async.detect([3,2,2,1,2], detectIterator.bind(this, call_order), function(result){
2259+
async.detect([3,2,2,1,2], detectIterator.bind(this, call_order), function(err, result){
22592260
call_order.push('callback');
2261+
test.equals(err, null);
22602262
test.equals(result, 2);
22612263
});
22622264
setTimeout(function(){
@@ -2267,8 +2269,9 @@ exports['detect - mulitple matches'] = function(test){
22672269

22682270
exports['detectSeries'] = function(test){
22692271
var call_order = [];
2270-
async.detectSeries([3,2,1], detectIterator.bind(this, call_order), function(result){
2272+
async.detectSeries([3,2,1], detectIterator.bind(this, call_order), function(err, result){
22712273
call_order.push('callback');
2274+
test.equals(err, null);
22722275
test.equals(result, 2);
22732276
});
22742277
setTimeout(function(){
@@ -2279,8 +2282,9 @@ exports['detectSeries'] = function(test){
22792282

22802283
exports['detectSeries - multiple matches'] = function(test){
22812284
var call_order = [];
2282-
async.detectSeries([3,2,2,1,2], detectIterator.bind(this, call_order), function(result){
2285+
async.detectSeries([3,2,2,1,2], detectIterator.bind(this, call_order), function(err, result){
22832286
call_order.push('callback');
2287+
test.equals(err, null);
22842288
test.equals(result, 2);
22852289
});
22862290
setTimeout(function(){
@@ -2292,8 +2296,9 @@ exports['detectSeries - multiple matches'] = function(test){
22922296
exports['detectSeries - ensure stop'] = function (test) {
22932297
async.detectSeries([1, 2, 3, 4, 5], function (num, cb) {
22942298
if (num > 3) throw new Error("detectSeries did not stop iterating");
2295-
cb(num === 3);
2296-
}, function (result) {
2299+
cb(null, num === 3);
2300+
}, function (err, result) {
2301+
test.equals(err, null);
22972302
test.equals(result, 3);
22982303
test.done();
22992304
});

0 commit comments

Comments
 (0)