UNPKG

3.2 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _once = require('./internal/once.js');
8
9var _once2 = _interopRequireDefault(_once);
10
11var _onlyOnce = require('./internal/onlyOnce.js');
12
13var _onlyOnce2 = _interopRequireDefault(_onlyOnce);
14
15var _wrapAsync = require('./internal/wrapAsync.js');
16
17var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
18
19var _awaitify = require('./internal/awaitify.js');
20
21var _awaitify2 = _interopRequireDefault(_awaitify);
22
23function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
24
25/**
26 * Runs the `tasks` array of functions in series, each passing their results to
27 * the next in the array. However, if any of the `tasks` pass an error to their
28 * own callback, the next function is not executed, and the main `callback` is
29 * immediately called with the error.
30 *
31 * @name waterfall
32 * @static
33 * @memberOf module:ControlFlow
34 * @method
35 * @category Control Flow
36 * @param {Array} tasks - An array of [async functions]{@link AsyncFunction}
37 * to run.
38 * Each function should complete with any number of `result` values.
39 * The `result` values will be passed as arguments, in order, to the next task.
40 * @param {Function} [callback] - An optional callback to run once all the
41 * functions have completed. This will be passed the results of the last task's
42 * callback. Invoked with (err, [results]).
43 * @returns {Promise} a promise, if a callback is omitted
44 * @example
45 *
46 * async.waterfall([
47 * function(callback) {
48 * callback(null, 'one', 'two');
49 * },
50 * function(arg1, arg2, callback) {
51 * // arg1 now equals 'one' and arg2 now equals 'two'
52 * callback(null, 'three');
53 * },
54 * function(arg1, callback) {
55 * // arg1 now equals 'three'
56 * callback(null, 'done');
57 * }
58 * ], function (err, result) {
59 * // result now equals 'done'
60 * });
61 *
62 * // Or, with named functions:
63 * async.waterfall([
64 * myFirstFunction,
65 * mySecondFunction,
66 * myLastFunction,
67 * ], function (err, result) {
68 * // result now equals 'done'
69 * });
70 * function myFirstFunction(callback) {
71 * callback(null, 'one', 'two');
72 * }
73 * function mySecondFunction(arg1, arg2, callback) {
74 * // arg1 now equals 'one' and arg2 now equals 'two'
75 * callback(null, 'three');
76 * }
77 * function myLastFunction(arg1, callback) {
78 * // arg1 now equals 'three'
79 * callback(null, 'done');
80 * }
81 */
82function waterfall(tasks, callback) {
83 callback = (0, _once2.default)(callback);
84 if (!Array.isArray(tasks)) return callback(new Error('First argument to waterfall must be an array of functions'));
85 if (!tasks.length) return callback();
86 var taskIndex = 0;
87
88 function nextTask(args) {
89 var task = (0, _wrapAsync2.default)(tasks[taskIndex++]);
90 task(...args, (0, _onlyOnce2.default)(next));
91 }
92
93 function next(err, ...args) {
94 if (err === false) return;
95 if (err || taskIndex === tasks.length) {
96 return callback(err, ...args);
97 }
98 nextTask(args);
99 }
100
101 nextTask([]);
102}
103
104exports.default = (0, _awaitify2.default)(waterfall);
105module.exports = exports.default;
\No newline at end of file