summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Early <alexander.early@gmail.com>2016-03-06 15:02:10 -0800
committerAlexander Early <alexander.early@gmail.com>2016-03-06 15:02:10 -0800
commit4d825639606b8db83c8e4c9c46b8e8771499071b (patch)
treeb2eeb570299083f6de1075ba15bb5f6a317860ba
parentf38483cbccc16efe205b0bf1439c233e61a7f090 (diff)
downloadasync-4d825639606b8db83c8e4c9c46b8e8771499071b.tar.gz
defend against multiple callbacks. Fixes #814
-rw-r--r--lib/auto.js5
-rw-r--r--mocha_test/auto.js14
2 files changed, 16 insertions, 3 deletions
diff --git a/lib/auto.js b/lib/auto.js
index 0e9d108..ae3ecfc 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 c6b7150..6b96be4 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();
+ });
+
});