You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
3.1 KiB
105 lines
3.1 KiB
'use strict'; |
|
|
|
Object.defineProperty(exports, "__esModule", { |
|
value: true |
|
}); |
|
|
|
var _once = require('./internal/once.js'); |
|
|
|
var _once2 = _interopRequireDefault(_once); |
|
|
|
var _onlyOnce = require('./internal/onlyOnce.js'); |
|
|
|
var _onlyOnce2 = _interopRequireDefault(_onlyOnce); |
|
|
|
var _wrapAsync = require('./internal/wrapAsync.js'); |
|
|
|
var _wrapAsync2 = _interopRequireDefault(_wrapAsync); |
|
|
|
var _awaitify = require('./internal/awaitify.js'); |
|
|
|
var _awaitify2 = _interopRequireDefault(_awaitify); |
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } |
|
|
|
/** |
|
* 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 {Promise} a promise, if a callback is omitted |
|
* @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'); |
|
* } |
|
*/ |
|
function waterfall(tasks, callback) { |
|
callback = (0, _once2.default)(callback); |
|
if (!Array.isArray(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++]); |
|
task(...args, (0, _onlyOnce2.default)(next)); |
|
} |
|
|
|
function next(err, ...args) { |
|
if (err === false) return; |
|
if (err || taskIndex === tasks.length) { |
|
return callback(err, ...args); |
|
} |
|
nextTask(args); |
|
} |
|
|
|
nextTask([]); |
|
} |
|
|
|
exports.default = (0, _awaitify2.default)(waterfall); |
|
module.exports = exports['default']; |