summaryrefslogtreecommitdiff
path: root/lib/waterfall.js
diff options
context:
space:
mode:
authorAlexander Early <alexander.early@gmail.com>2017-04-03 23:46:20 -0700
committerAlexander Early <alexander.early@gmail.com>2017-04-03 23:46:20 -0700
commit53019a85063576c3aa7e48410d5f8018337f348e (patch)
treefd0672a815476acfacbf16b441aba918b8f49ffc /lib/waterfall.js
parentdddb9f50a079580f938e69f1ba9d32927a08301c (diff)
downloadasync-53019a85063576c3aa7e48410d5f8018337f348e.tar.gz
simplify waterfall, commit built files
Diffstat (limited to 'lib/waterfall.js')
-rw-r--r--lib/waterfall.js28
1 files changed, 13 insertions, 15 deletions
diff --git a/lib/waterfall.js b/lib/waterfall.js
index d5245c3..e4c033a 100644
--- a/lib/waterfall.js
+++ b/lib/waterfall.js
@@ -68,25 +68,23 @@ export default function(tasks, callback) {
if (!isArray(tasks)) return callback(new Error('First argument to waterfall must be an array of functions'));
if (!tasks.length) return callback();
var taskIndex = 0;
+ var args = [];
- function nextTask(args) {
- if (taskIndex === tasks.length) {
- return callback.apply(null, [null].concat(args));
- }
-
- var taskCallback = onlyOnce(function(err/*, cbArgs...*/) {
- var cbArgs = slice(arguments, 1);
- if (err) {
- return callback.apply(null, [err].concat(cbArgs));
- }
- nextTask(cbArgs);
- });
-
+ function nextTask() {
+ var task = wrapAsync(tasks[taskIndex++]);
+ var taskCallback = onlyOnce(next);
args.push(taskCallback);
- var task = wrapAsync(tasks[taskIndex++]);
task.apply(null, args);
}
- nextTask([]);
+ function next(err/*, ...args*/) {
+ if (err || taskIndex === tasks.length) {
+ return callback.apply(null, arguments);
+ }
+ args = slice(arguments, 1);
+ nextTask();
+ }
+
+ nextTask();
}