Skip to content

Commit

Permalink
make eachSeries sync w/ single item and a sync iterator. Fixes #782
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Early committed Jun 8, 2015
1 parent 392afe1 commit 0450c3b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
16 changes: 10 additions & 6 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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();
}
}
}
}));
Expand Down
21 changes: 17 additions & 4 deletions test/test-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 0450c3b

Please sign in to comment.