| [9af201e] | 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 |
|
|---|
| 7 | var _once = require('./internal/once.js');
|
|---|
| 8 |
|
|---|
| 9 | var _once2 = _interopRequireDefault(_once);
|
|---|
| 10 |
|
|---|
| 11 | var _wrapAsync = require('./internal/wrapAsync.js');
|
|---|
| 12 |
|
|---|
| 13 | var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
|
|---|
| 14 |
|
|---|
| 15 | var _awaitify = require('./internal/awaitify.js');
|
|---|
| 16 |
|
|---|
| 17 | var _awaitify2 = _interopRequireDefault(_awaitify);
|
|---|
| 18 |
|
|---|
| 19 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|---|
| 20 |
|
|---|
| 21 | /**
|
|---|
| 22 | * Runs the `tasks` array of functions in parallel, without waiting until the
|
|---|
| 23 | * previous function has completed. Once any of the `tasks` complete or pass an
|
|---|
| 24 | * error to its callback, the main `callback` is immediately called. It's
|
|---|
| 25 | * equivalent to `Promise.race()`.
|
|---|
| 26 | *
|
|---|
| 27 | * @name race
|
|---|
| 28 | * @static
|
|---|
| 29 | * @memberOf module:ControlFlow
|
|---|
| 30 | * @method
|
|---|
| 31 | * @category Control Flow
|
|---|
| 32 | * @param {Array} tasks - An array containing [async functions]{@link AsyncFunction}
|
|---|
| 33 | * to run. Each function can complete with an optional `result` value.
|
|---|
| 34 | * @param {Function} callback - A callback to run once any of the functions have
|
|---|
| 35 | * completed. This function gets an error or result from the first function that
|
|---|
| 36 | * completed. Invoked with (err, result).
|
|---|
| 37 | * @returns {Promise} a promise, if a callback is omitted
|
|---|
| 38 | * @example
|
|---|
| 39 | *
|
|---|
| 40 | * async.race([
|
|---|
| 41 | * function(callback) {
|
|---|
| 42 | * setTimeout(function() {
|
|---|
| 43 | * callback(null, 'one');
|
|---|
| 44 | * }, 200);
|
|---|
| 45 | * },
|
|---|
| 46 | * function(callback) {
|
|---|
| 47 | * setTimeout(function() {
|
|---|
| 48 | * callback(null, 'two');
|
|---|
| 49 | * }, 100);
|
|---|
| 50 | * }
|
|---|
| 51 | * ],
|
|---|
| 52 | * // main callback
|
|---|
| 53 | * function(err, result) {
|
|---|
| 54 | * // the result will be equal to 'two' as it finishes earlier
|
|---|
| 55 | * });
|
|---|
| 56 | */
|
|---|
| 57 | function race(tasks, callback) {
|
|---|
| 58 | callback = (0, _once2.default)(callback);
|
|---|
| 59 | if (!Array.isArray(tasks)) return callback(new TypeError('First argument to race must be an array of functions'));
|
|---|
| 60 | if (!tasks.length) return callback();
|
|---|
| 61 | for (var i = 0, l = tasks.length; i < l; i++) {
|
|---|
| 62 | (0, _wrapAsync2.default)(tasks[i])(callback);
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | exports.default = (0, _awaitify2.default)(race, 2);
|
|---|
| 67 | module.exports = exports.default; |
|---|