JAVASCRIPT   8

waterfall.js

Guest on 21st May 2021 06:47:23 AM

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

Raw Paste


Login or Register to edit or fork this paste. It's free.