waterfall.js 3.3 KB

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