Projekt

Obecné

Profil

Stáhnout (2.11 KB) Statistiky
| Větev: | Revize:
1
'use strict';
2

    
3
Object.defineProperty(exports, "__esModule", {
4
    value: true
5
});
6
exports.default = race;
7

    
8
var _isArray = require('lodash/isArray');
9

    
10
var _isArray2 = _interopRequireDefault(_isArray);
11

    
12
var _noop = require('lodash/noop');
13

    
14
var _noop2 = _interopRequireDefault(_noop);
15

    
16
var _once = require('./internal/once');
17

    
18
var _once2 = _interopRequireDefault(_once);
19

    
20
var _wrapAsync = require('./internal/wrapAsync');
21

    
22
var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
23

    
24
function _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
 */
62
function 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
}
70
module.exports = exports['default'];
(75-75/105)