3 Object.defineProperty(exports, "__esModule", {
7 exports.default = function (tasks, callback) {
8 callback = (0, _once2.default)(callback || _noop2.default);
9 if (!(0, _isArray2.default)(tasks)) return callback(new Error('First argument to waterfall must be an array of functions'));
10 if (!tasks.length) return callback();
13 function nextTask(args) {
14 if (taskIndex === tasks.length) {
15 return callback.apply(null, [null].concat(args));
18 var taskCallback = (0, _onlyOnce2.default)((0, _rest2.default)(function (err, args) {
20 return callback.apply(null, [err].concat(args));
25 args.push(taskCallback);
27 var task = tasks[taskIndex++];
28 task.apply(null, args);
34 var _isArray = require('lodash/isArray');
36 var _isArray2 = _interopRequireDefault(_isArray);
38 var _noop = require('lodash/noop');
40 var _noop2 = _interopRequireDefault(_noop);
42 var _once = require('./internal/once');
44 var _once2 = _interopRequireDefault(_once);
46 var _rest = require('./internal/rest');
48 var _rest2 = _interopRequireDefault(_rest);
50 var _onlyOnce = require('./internal/onlyOnce');
52 var _onlyOnce2 = _interopRequireDefault(_onlyOnce);
54 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
56 module.exports = exports['default'];
59 * Runs the `tasks` array of functions in series, each passing their results to
60 * the next in the array. However, if any of the `tasks` pass an error to their
61 * own callback, the next function is not executed, and the main `callback` is
62 * immediately called with the error.
66 * @memberOf module:ControlFlow
68 * @category Control Flow
69 * @param {Array} tasks - An array of functions to run, each function is passed
70 * a `callback(err, result1, result2, ...)` it must call on completion. The
71 * first argument is an error (which can be `null`) and any further arguments
72 * will be passed as arguments in order to the next task.
73 * @param {Function} [callback] - An optional callback to run once all the
74 * functions have completed. This will be passed the results of the last task's
75 * callback. Invoked with (err, [results]).
80 * function(callback) {
81 * callback(null, 'one', 'two');
83 * function(arg1, arg2, callback) {
84 * // arg1 now equals 'one' and arg2 now equals 'two'
85 * callback(null, 'three');
87 * function(arg1, callback) {
88 * // arg1 now equals 'three'
89 * callback(null, 'done');
91 * ], function (err, result) {
92 * // result now equals 'done'
95 * // Or, with named functions:
100 * ], function (err, result) {
101 * // result now equals 'done'
103 * function myFirstFunction(callback) {
104 * callback(null, 'one', 'two');
106 * function mySecondFunction(arg1, arg2, callback) {
107 * // arg1 now equals 'one' and arg2 now equals 'two'
108 * callback(null, 'three');
110 * function myLastFunction(arg1, callback) {
111 * // arg1 now equals 'three'
112 * callback(null, 'done');