diff --git a/lib/async.js b/lib/async.js index 0589349d9..07ffd8423 100644 --- a/lib/async.js +++ b/lib/async.js @@ -247,9 +247,9 @@ callback = _once(callback || noop); obj = obj || []; var nextKey = _keyIterator(obj); + var key = nextKey(); function iterate() { var sync = true; - var key = nextKey(); if (key === null) { return callback(null); } @@ -258,11 +258,15 @@ callback(err); } else { - if (sync) { - async.nextTick(iterate); - } - else { - iterate(); + key = nextKey(); + if (key === null) { + return callback(null); + } else { + if (sync) { + async.nextTick(iterate); + } else { + iterate(); + } } } })); diff --git a/test/test-async.js b/test/test-async.js index 88fa9ae8f..fc3972c0a 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -1502,6 +1502,19 @@ exports['eachSeries array modification'] = function(test) { setTimeout(test.done, 50); }; +// bug #782. Remove in next major release +exports['eachSeries single item'] = function (test) { + test.expect(1); + var sync = true; + async.eachSeries([1], function (i, cb) { + cb(null); + }, function () { + test.ok(sync, "callback not called on same tick"); + }); + sync = false; + test.done(); +}; + exports['eachSeries error'] = function(test){ test.expect(2); var call_order = []; @@ -2901,21 +2914,21 @@ exports['queue'] = { // the concurrency to 2. Wait again for a later loop then verify the concurrency. // Repeat that one more time by chaning the concurrency to 5. 'changing concurrency': function (test) { - + var q = async.queue(function(task, callback){ setTimeout(function(){ callback(); }, 100); }, 1); - + for(var i = 0; i < 50; i++){ q.push(''); } - + q.drain = function(){ test.done(); }; - + setTimeout(function(){ test.equal(q.concurrency, 1); q.concurrency = 2;