Skip to content

Commit

Permalink
added asyncRejectSeries from async.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Caolan McMahon committed Dec 1, 2010
1 parent 817b978 commit 55820c0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
29 changes: 29 additions & 0 deletions test/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,33 @@ exports['async: asyncSelectSeries alias'] = function(test){
test.done();
};

exports['async: asyncReject'] = function(test){
_.asyncReject([3,1,2], function(x, callback){
setTimeout(function(){callback(x % 2);}, x*25);
}, function(results){
test.same(results, [2]);
test.done();
});
};

exports['async: asyncReject original untouched'] = function(test){
var a = [3,1,2];
_.asyncReject(a, function(x, callback){
callback(x % 2);
}, function(results){
test.same(results, [2]);
test.same(a, [3,1,2]);
test.done();
});
};

exports['async: asyncRejectSeries'] = function(test){
_.asyncRejectSeries([3,1,2], function(x, callback){
setTimeout(function(){callback(x % 2);}, x*25);
}, function(results){
test.same(results, [2]);
test.done();
});
};

})(typeof exports === 'undefined' ? this['async_tests'] = {}: exports);
24 changes: 24 additions & 0 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,30 @@
_.asyncSelectSeries = _.asyncFilterSeries;


var _asyncReject = function (eachfn, arr, iterator, callback) {
var results = [];
arr = _.map(arr, function (x, i) {
return {index: i, value: x};
});
eachfn(arr, function (x, callback) {
iterator(x.value, function (v) {
if (!v) {
results.push(x);
}
callback();
});
}, function (err) {
callback(_.map(results.sort(function (a, b) {
return a.index - b.index;
}), function (x) {
return x.value;
}));
});
};
_.asyncReject = doParallel(_asyncReject);
_.asyncRejectSeries = doSeries(_asyncReject);


// The OOP Wrapper
// ---------------

Expand Down

0 comments on commit 55820c0

Please sign in to comment.