Skip to content

Commit

Permalink
filterWithError
Browse files Browse the repository at this point in the history
  • Loading branch information
gyllstromk committed Jun 24, 2013
1 parent c99537a commit 2aa2f82
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,24 @@
};
async.forEach = async.each;

async.filterWithError = function (iterable, iterator, callback) {
async.map(iterable, function (each, callback) {
iterator(each, function (err, condition) {
callback(null, { each: each, condition: condition });
});
}, function (err, results) {
if (err) {
return callback(err);
}

callback(null, results.filter(function (each) {
return each.condition;
}).map(function (each) {
return each.each;
}));
});
};

async.eachSeries = function (arr, iterator, callback) {
callback = callback || function () {};
if (!arr.length) {
Expand Down
12 changes: 12 additions & 0 deletions test/test-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,18 @@ exports['filterSeries'] = function(test){
});
};

exports['filterWithError'] = function(test){
async.filterWithError([3,1,2], function (each, callback) {
filterIterator(each, function (result) {
callback(null, result);
});
}, function(err, results){
test.equals(err, null);
test.same(results, [3,1]);
test.done();
});
};

exports['select alias'] = function(test){
test.equals(async.select, async.filter);
test.done();
Expand Down

0 comments on commit 2aa2f82

Please sign in to comment.