Skip to content

Commit

Permalink
simplified implementation of series
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Early committed May 31, 2015
1 parent 48e9a76 commit c3d4c13
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 40 deletions.
40 changes: 13 additions & 27 deletions lib/async.js
Expand Up @@ -350,7 +350,7 @@
async.inject =
async.foldl =
async.reduce = function (arr, memo, iterator, callback) {
async.eachSeries(arr, function (x, callback) {
async.eachOfSeries(arr, function (x, i, callback) {
iterator(memo, x, function (err, v) {
memo = v;
callback(err);
Expand Down Expand Up @@ -682,34 +682,20 @@

async.series = function (tasks, callback) {
callback = callback || noop;
if (_isArray(tasks)) {
async.mapSeries(tasks, function (fn, callback) {
if (fn) {
fn(function (err) {
var args = _baseSlice(arguments, 1);
if (args.length <= 1) {
args = args[0];
}
callback.call(null, err, args);
});
var results = _isArrayLike(tasks) ? [] : {};

async.eachOfSeries(tasks, function (task, key, callback) {
task(function (err) {
var args = _baseSlice(arguments, 1);
if (args.length <= 1) {
args = args[0];
}
}, callback);
}
else {
var results = {};
async.eachSeries(_keys(tasks), function (k, callback) {
tasks[k](function (err) {
var args = _baseSlice(arguments, 1);
if (args.length <= 1) {
args = args[0];
}
results[k] = args;
callback(err);
});
}, function (err) {
callback(err, results);
results[key] = args;
callback(err);
});
}
}, function (err) {
callback(err, results);
});
};

async.iterator = function (tasks) {
Expand Down
30 changes: 17 additions & 13 deletions test/test-async.js
Expand Up @@ -1100,7 +1100,9 @@ exports['parallel does not continue replenishing after error'] = function (test)
};


exports['series'] = function(test){
exports['series'] = {

'series': function(test){
var call_order = [];
async.series([
function(callback){
Expand Down Expand Up @@ -1128,17 +1130,17 @@ exports['series'] = function(test){
test.same(call_order, [1,2,3]);
test.done();
});
};
},

exports['series empty array'] = function(test){
'empty array': function(test){
async.series([], function(err, results){
test.equals(err, null);
test.same(results, []);
test.done();
});
};
},

exports['series error'] = function(test){
'error': function(test){
test.expect(1);
async.series([
function(callback){
Expand All @@ -1153,16 +1155,16 @@ exports['series error'] = function(test){
test.equals(err, 'error');
});
setTimeout(test.done, 100);
};
},

exports['series no callback'] = function(test){
'no callback': function(test){
async.series([
function(callback){callback();},
function(callback){callback(); test.done();},
]);
};
},

exports['series object'] = function(test){
'object': function(test){
var call_order = [];
async.series(getFunctionsObject(call_order), function(err, results){
test.equals(err, null);
Expand All @@ -1174,9 +1176,9 @@ exports['series object'] = function(test){
test.same(call_order, [1,2,3]);
test.done();
});
};
},

exports['series call in another context'] = function(test) {
'call in another context': function(test) {
if (typeof process === 'undefined') {
// node only test
test.done();
Expand All @@ -1200,10 +1202,10 @@ exports['series call in another context'] = function(test) {
}).toString() + "())";

vm.runInNewContext(fn, sandbox);
};
},

// Issue 10 on github: https://github.com/caolan/async/issues#issue/10
exports['series falsy return values'] = function (test) {
'falsy return values': function (test) {
function taskFalse(callback) {
async.nextTick(function() {
callback(null, false);
Expand Down Expand Up @@ -1235,6 +1237,8 @@ exports['series falsy return values'] = function (test) {
test.done();
}
);
}

};


Expand Down

0 comments on commit c3d4c13

Please sign in to comment.