Blame view

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