diff options
| author | Caolan McMahon <caolan@caolanmcmahon.com> | 2011-05-17 11:36:30 -0700 |
|---|---|---|
| committer | Caolan McMahon <caolan@caolanmcmahon.com> | 2011-05-17 11:36:30 -0700 |
| commit | ca4db01269f031322175da2f1f6bec741d6b9dbc (patch) | |
| tree | 4b455901c0d1af2888feceb65407bc8ec1d09288 | |
| parent | ec90dc184eb45dfa8bdede1b21b47da07709adc7 (diff) | |
| download | async-ca4db01269f031322175da2f1f6bec741d6b9dbc.tar.gz | |
fix parallel implementation of detect when multiple matches occur, closes #28
| -rw-r--r-- | lib/async.js | 1 | ||||
| -rw-r--r-- | test/test-async.js | 44 |
2 files changed, 40 insertions, 5 deletions
diff --git a/lib/async.js b/lib/async.js index 8c863ac..67de211 100644 --- a/lib/async.js +++ b/lib/async.js @@ -247,6 +247,7 @@ iterator(x, function (result) { if (result) { main_callback(x); + main_callback = function () {}; } else { callback(); diff --git a/test/test-async.js b/test/test-async.js index 8c2cebd..4a7f986 100644 --- a/test/test-async.js +++ b/test/test-async.js @@ -181,19 +181,19 @@ exports['parallel'] = function(test){ setTimeout(function(){ call_order.push(1); callback(null, 1); - }, 25); + }, 50); }, function(callback){ setTimeout(function(){ call_order.push(2); callback(null, 2); - }, 50); + }, 100); }, function(callback){ setTimeout(function(){ call_order.push(3); callback(null, 3,3); - }, 15); + }, 25); } ], function(err, results){ @@ -773,6 +773,23 @@ exports['detect'] = function(test){ }, 100); }; +exports['detect - mulitple matches'] = function(test){ + var call_order = []; + async.detect([3,2,2,1,2], function(x, callback){ + setTimeout(function(){ + call_order.push(x); + callback(x == 2); + }, x*25); + }, function(result){ + call_order.push('callback'); + test.equals(result, 2); + }); + setTimeout(function(){ + test.same(call_order, [1,2,'callback',2,2,3]); + test.done(); + }, 100); +}; + exports['detectSeries'] = function(test){ var call_order = []; async.detectSeries([3,2,1], function(x, callback){ @@ -790,6 +807,23 @@ exports['detectSeries'] = function(test){ }, 200); }; +exports['detectSeries - multiple matches'] = function(test){ + var call_order = []; + async.detectSeries([3,2,2,1,2], function(x, callback){ + setTimeout(function(){ + call_order.push(x); + callback(x == 2); + }, x*25); + }, function(result){ + call_order.push('callback'); + test.equals(result, 2); + }); + setTimeout(function(){ + test.same(call_order, [3,2,'callback']); + test.done(); + }, 200); +}; + exports['sortBy'] = function(test){ async.sortBy([{a:1},{a:15},{a:6}], function(x, callback){ setTimeout(function(){callback(null, x.a);}, 0); @@ -1275,7 +1309,7 @@ exports['falsy return values in series'] = function (test) { async.series( [taskFalse, taskUndefined, taskEmpty, taskNull], function(err, results) { - test.same(results, [false, undefined, undefined, null]); + test.equal(results.length, 4); test.strictEqual(results[0], false); test.strictEqual(results[1], undefined); test.strictEqual(results[2], undefined); @@ -1310,7 +1344,7 @@ exports['falsy return values in parallel'] = function (test) { async.parallel( [taskFalse, taskUndefined, taskEmpty, taskNull], function(err, results) { - test.same(results, [false, undefined, undefined, null]); + test.equal(results.length, 4); test.strictEqual(results[0], false); test.strictEqual(results[1], undefined); test.strictEqual(results[2], undefined); |
