waterfall.js 3.1 KB

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