source: trip-planner-front/node_modules/async/race.js@ 188ee53

Last change on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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