From 4d825639606b8db83c8e4c9c46b8e8771499071b Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Sun, 6 Mar 2016 15:02:10 -0800 Subject: [PATCH] defend against multiple callbacks. Fixes #814 --- lib/auto.js | 5 +++-- mocha_test/auto.js | 14 +++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/auto.js b/lib/auto.js index 0e9d10867..ae3ecfcf8 100644 --- a/lib/auto.js +++ b/lib/auto.js @@ -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'; @@ -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]; @@ -80,7 +81,7 @@ export default function (tasks, concurrency, callback) { results[k] = args; setImmediate(taskComplete); } - }); + })); var requires = task.slice(0, task.length - 1); diff --git a/mocha_test/auto.js b/mocha_test/auto.js index c6b7150ea..6b96be4fc 100644 --- a/mocha_test/auto.js +++ b/mocha_test/auto.js @@ -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){ @@ -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(); + }); + });