summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Early <aearly@fluid.com>2015-05-31 01:47:02 -0700
committerAlexander Early <aearly@fluid.com>2015-05-31 01:47:02 -0700
commitc3d4c13012ab592d128f30331c8281ee2aec69ad (patch)
treeef555db2a6d4a378e4433f63064ac759438bc8f2
parent48e9a76c53d36e15499df41a5a5c119d2d9eb53d (diff)
downloadasync-c3d4c13012ab592d128f30331c8281ee2aec69ad.tar.gz
simplified implementation of series
-rw-r--r--lib/async.js40
-rwxr-xr-xtest/test-async.js30
2 files changed, 30 insertions, 40 deletions
diff --git a/lib/async.js b/lib/async.js
index e3310d5..dec5939 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -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);
@@ -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) {
diff --git a/test/test-async.js b/test/test-async.js
index b0be295..7c8fe30 100755
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -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){
@@ -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){
@@ -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);
@@ -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();
@@ -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);
@@ -1235,6 +1237,8 @@ exports['series falsy return values'] = function (test) {
test.done();
}
);
+}
+
};