Skip to content

Commit

Permalink
defend against multiple callbacks. Fixes #814
Browse files Browse the repository at this point in the history
  • Loading branch information
aearly committed Mar 6, 2016
1 parent f38483c commit 4d82563
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/auto.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import okeys from 'lodash/keys';
import noop from 'lodash/noop';
import once from 'lodash/once';
import rest from 'lodash/rest';
import onlyOnce from './internal/onlyOnce';

import setImmediate from './internal/setImmediate';

Expand Down Expand Up @@ -60,7 +61,7 @@ export default function (tasks, concurrency, callback) {
arrayEach(keys, function (k) {
if (hasError) return;
var task = isArray(tasks[k]) ? tasks[k]: [tasks[k]];
var taskCallback = rest(function(err, args) {
var taskCallback = onlyOnce(rest(function(err, args) {
runningTasks--;
if (args.length <= 1) {
args = args[0];
Expand All @@ -80,7 +81,7 @@ export default function (tasks, concurrency, callback) {
results[k] = args;
setImmediate(taskComplete);
}
});
}));

var requires = task.slice(0, task.length - 1);

Expand Down
14 changes: 13 additions & 1 deletion mocha_test/auto.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var _ = require('lodash');

describe('auto', function () {

it('auto', function(done){
it('basics', function(done){
var callOrder = [];
async.auto({
task1: ['task2', function(results, callback){
Expand Down Expand Up @@ -344,4 +344,16 @@ describe('auto', function () {
});
});

it("does not allow calling callbacks twice", function () {
expect(function () {
async.auto({
bad: function (cb) {
cb();
cb();
}
}, function () {});

}).to.throw();
});

});

0 comments on commit 4d82563

Please sign in to comment.