Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow no callback for seq #618

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,14 @@
return function () {
var that = this;
var args = Array.prototype.slice.call(arguments);
var callback = args.pop();

var callback = args.slice(-1)[0];
if (typeof callback == 'function') {
args.pop();
} else {
callback = null;
}

async.reduce(fns, args, function (newargs, fn, cb) {
fn.apply(that, newargs.concat([function () {
var err = arguments[0];
Expand All @@ -1065,7 +1072,9 @@
}]))
},
function (err, results) {
callback.apply(that, [err].concat(results));
if (callback) {
callback.apply(that, [err].concat(results));
}
});
};
};
Expand Down
21 changes: 21 additions & 0 deletions test/test-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,27 @@ exports['seq binding'] = function (test) {
});
};

exports['seq without callback'] = function (test) {
test.expect(2);
var testerr = new Error('test');
var testcontext = {name: 'foo'};

var add2 = function (n, cb) {
test.equal(this, testcontext);
setTimeout(function () {
cb(null, n + 2);
}, 50);
};
var mul3 = function (n, cb) {
test.equal(this, testcontext);
setTimeout(function () {
test.done();
}, 15);
};
var add2mul3 = async.seq(add2, mul3);
add2mul3.call(testcontext, 3);
}

exports['auto'] = function(test){
var callOrder = [];
var testdata = [{test: 'test'}];
Expand Down