Skip to content

Commit

Permalink
Merge pull request #866 from markyen/detect-limit
Browse files Browse the repository at this point in the history
Add detectLimit
  • Loading branch information
aearly committed Jul 31, 2015
2 parents 1f97538 + f353d7a commit 7cb31f8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@
}
async.detect = _createTester(async.eachOf, identity, _findGetResult);
async.detectSeries = _createTester(async.eachOfSeries, identity, _findGetResult);
async.detectLimit = _createTester(async.eachOfLimit, identity, _findGetResult);

async.sortBy = function (arr, iterator, callback) {
async.map(arr, function (x, callback) {
Expand Down
37 changes: 37 additions & 0 deletions test/test-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -2329,6 +2329,43 @@ exports['detectSeries - ensure stop'] = function (test) {
});
};

exports['detectLimit'] = function(test){
test.expect(2);
var call_order = [];
async.detectLimit([3, 2, 1], 2, detectIterator.bind(this, call_order), function(result) {
call_order.push('callback');
test.equals(result, 2);
});
setTimeout(function() {
test.same(call_order, [2, 'callback', 3]);
test.done();
}, 100);
};

exports['detectLimit - multiple matches'] = function(test){
test.expect(2);
var call_order = [];
async.detectLimit([3,2,2,1,2], 2, detectIterator.bind(this, call_order), function(result){
call_order.push('callback');
test.equals(result, 2);
});
setTimeout(function(){
test.same(call_order, [2, 'callback', 3]);
test.done();
}, 100);
};

exports['detectLimit - ensure stop'] = function (test) {
test.expect(1);
async.detectLimit([1, 2, 3, 4, 5], 2, function (num, cb) {
if (num > 4) throw new Error("detectLimit did not stop iterating");
cb(num === 3);
}, function (result) {
test.equals(result, 3);
test.done();
});
};

exports['sortBy'] = function(test){
test.expect(2);

Expand Down

0 comments on commit 7cb31f8

Please sign in to comment.