UI for Zipcoin Blue

waterfall.js 3.3KB

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