diff --git a/test/async.js b/test/async.js index 36ecfafa3..b9475b3b0 100644 --- a/test/async.js +++ b/test/async.js @@ -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); diff --git a/underscore.js b/underscore.js index 26cb929b7..7ca6b0e9a 100644 --- a/underscore.js +++ b/underscore.js @@ -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 // ---------------