summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAlexander Early <aearly@fluid.com>2015-06-01 00:23:25 -0700
committerAlexander Early <aearly@fluid.com>2015-06-01 00:23:25 -0700
commitba53a8ad87b839550dc8a648afb17d904e37d207 (patch)
tree002726ee61ec3a96f29c1e9b6755cb048e21e80b /lib
parent9406b643f1ea1d142b1142c9c1ad114c1d04ef85 (diff)
downloadasync-ba53a8ad87b839550dc8a648afb17d904e37d207.tar.gz
make use of _once() function, rather than assigning the callback to noop
Diffstat (limited to 'lib')
-rw-r--r--lib/async.js38
1 files changed, 17 insertions, 21 deletions
diff --git a/lib/async.js b/lib/async.js
index 68b9c54..58b11d9 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -37,7 +37,16 @@
return function() {
if (called) throw new Error("Callback was already called.");
called = true;
- fn.apply(root, arguments);
+ fn.apply(this, arguments);
+ };
+ }
+
+ function _once(fn) {
+ var called = false;
+ return function() {
+ if (called) return;
+ called = true;
+ fn.apply(this, arguments);
};
}
@@ -206,7 +215,7 @@
async.forEachOf =
async.eachOf = function (object, iterator, callback) {
- callback = callback || noop;
+ callback = _once(callback || noop);
object = object || [];
var size = object.length || _keys(object).length;
var completed = 0;
@@ -219,7 +228,6 @@
function done(err) {
if (err) {
callback(err);
- callback = noop;
}
else {
completed += 1;
@@ -232,7 +240,7 @@
async.forEachOfSeries =
async.eachOfSeries = function (obj, iterator, callback) {
- callback = callback || noop;
+ callback = _once(callback || noop);
obj = obj || [];
var nextKey = _keyIterator(obj);
function iterate() {
@@ -244,7 +252,6 @@
iterator(obj[key], key, function (err) {
if (err) {
callback(err);
- callback = noop;
}
else {
if (sync) {
@@ -270,7 +277,7 @@
function _eachOfLimit(limit) {
return function (obj, iterator, callback) {
- callback = callback || noop;
+ callback = _once(callback || noop);
obj = obj || [];
var nextKey = _keyIterator(obj);
if (limit <= 0) {
@@ -282,9 +289,7 @@
(function replenish () {
if (done && running <= 0) {
- callback(null);
- callback = noop;
- return;
+ return callback(null);
}
while (running < limit && !errored) {
@@ -293,7 +298,6 @@
done = true;
if (running <= 0) {
callback(null);
- callback = noop;
}
return;
}
@@ -303,7 +307,6 @@
if (err) {
callback(err);
errored = true;
- callback = noop;
}
else {
replenish();
@@ -504,7 +507,7 @@
};
async.auto = function (tasks, callback) {
- callback = callback || noop;
+ callback = _once(callback || noop);
var keys = _keys(tasks);
var remainingTasks = keys.length;
if (!remainingTasks) {
@@ -534,11 +537,7 @@
addListener(function () {
if (!remainingTasks) {
- var theCallback = callback;
- // prevent final callback from calling itself if it errors
- callback = noop;
-
- theCallback(null, results);
+ callback(null, results);
}
});
@@ -556,8 +555,6 @@
});
safeResults[k] = args;
callback(err, safeResults);
- // stop subsequent errors hitting callback multiple times
- callback = noop;
}
else {
results[k] = args;
@@ -631,7 +628,7 @@
};
async.waterfall = function (tasks, callback) {
- callback = callback || noop;
+ callback = _once(callback || noop);
if (!_isArray(tasks)) {
var err = new Error('First argument to waterfall must be an array of functions');
return callback(err);
@@ -643,7 +640,6 @@
return function (err) {
if (err) {
callback.apply(null, arguments);
- callback = noop;
}
else {
var args = _baseSlice(arguments, 1);