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
Conflicts:
	test/test-async.js
  • Loading branch information
Alexander Early committed Jun 8, 2015
1 parent 88906aa commit 0ad1237
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,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 @@ -254,11 +254,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
13 changes: 13 additions & 0 deletions test/test-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,19 @@ exports['eachSeries array modification'] = function(test) {
setTimeout(test.done, 25);
};

// 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

0 comments on commit 0ad1237

Please sign in to comment.