3 Object.defineProperty(exports, "__esModule", {
6 exports.default = race;
8 var _isArray = require('lodash/isArray');
10 var _isArray2 = _interopRequireDefault(_isArray);
12 var _noop = require('lodash/noop');
14 var _noop2 = _interopRequireDefault(_noop);
16 var _once = require('./internal/once');
18 var _once2 = _interopRequireDefault(_once);
20 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23 * Runs the `tasks` array of functions in parallel, without waiting until the
24 * previous function has completed. Once any of the `tasks` complete or pass an
25 * error to its callback, the main `callback` is immediately called. It's
26 * equivalent to `Promise.race()`.
30 * @memberOf module:ControlFlow
32 * @category Control Flow
33 * @param {Array} tasks - An array containing functions to run. Each function
34 * is passed a `callback(err, result)` which it must call on completion with an
35 * error `err` (which can be `null`) and an optional `result` value.
36 * @param {Function} callback - A callback to run once any of the functions have
37 * completed. This function gets an error or result from the first function that
38 * completed. Invoked with (err, result).
43 * function(callback) {
44 * setTimeout(function() {
45 * callback(null, 'one');
48 * function(callback) {
49 * setTimeout(function() {
50 * callback(null, 'two');
55 * function(err, result) {
56 * // the result will be equal to 'two' as it finishes earlier
59 function race(tasks, callback) {
60 callback = (0, _once2.default)(callback || _noop2.default);
61 if (!(0, _isArray2.default)(tasks)) return callback(new TypeError('First argument to race must be an array of functions'));
62 if (!tasks.length) return callback();
63 for (var i = 0, l = tasks.length; i < l; i++) {
67 module.exports = exports['default'];