diff --git a/lib/async.js b/lib/async.js index 9c2b6afbf..2a4b1e1ed 100644 --- a/lib/async.js +++ b/lib/async.js @@ -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) { diff --git a/test/test-async.js b/test/test-async.js index dc7d9497c..18894b0d3 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -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);