parallel.js
1 'use strict'; 2 3 Object.defineProperty(exports, "__esModule", { 4 value: true 5 }); 6 exports.default = parallel; 7 8 var _eachOf = require('./eachOf'); 9 10 var _eachOf2 = _interopRequireDefault(_eachOf); 11 12 var _parallel2 = require('./internal/parallel'); 13 14 var _parallel3 = _interopRequireDefault(_parallel2); 15 16 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 17 18 /** 19 * Run the `tasks` collection of functions in parallel, without waiting until 20 * the previous function has completed. If any of the functions pass an error to 21 * its callback, the main `callback` is immediately called with the value of the 22 * error. Once the `tasks` have completed, the results are passed to the final 23 * `callback` as an array. 24 * 25 * **Note:** `parallel` is about kicking-off I/O tasks in parallel, not about 26 * parallel execution of code. If your tasks do not use any timers or perform 27 * any I/O, they will actually be executed in series. Any synchronous setup 28 * sections for each task will happen one after the other. JavaScript remains 29 * single-threaded. 30 * 31 * **Hint:** Use [`reflect`]{@link module:Utils.reflect} to continue the 32 * execution of other tasks when a task fails. 33 * 34 * It is also possible to use an object instead of an array. Each property will 35 * be run as a function and the results will be passed to the final `callback` 36 * as an object instead of an array. This can be a more readable way of handling 37 * results from {@link async.parallel}. 38 * 39 * @name parallel 40 * @static 41 * @memberOf module:ControlFlow 42 * @method 43 * @category Control Flow 44 * @param {Array|Iterable|AsyncIterable|Object} tasks - A collection of 45 * [async functions]{@link AsyncFunction} to run. 46 * Each async function can complete with any number of optional `result` values. 47 * @param {Function} [callback] - An optional callback to run once all the 48 * functions have completed successfully. This function gets a results array 49 * (or object) containing all the result arguments passed to the task callbacks. 50 * Invoked with (err, results). 51 * @returns {Promise} a promise, if a callback is not passed 52 * 53 * @example 54 * async.parallel([ 55 * function(callback) { 56 * setTimeout(function() { 57 * callback(null, 'one'); 58 * }, 200); 59 * }, 60 * function(callback) { 61 * setTimeout(function() { 62 * callback(null, 'two'); 63 * }, 100); 64 * } 65 * ], 66 * // optional callback 67 * function(err, results) { 68 * // the results array will equal ['one','two'] even though 69 * // the second function had a shorter timeout. 70 * }); 71 * 72 * // an example using an object instead of an array 73 * async.parallel({ 74 * one: function(callback) { 75 * setTimeout(function() { 76 * callback(null, 1); 77 * }, 200); 78 * }, 79 * two: function(callback) { 80 * setTimeout(function() { 81 * callback(null, 2); 82 * }, 100); 83 * } 84 * }, function(err, results) { 85 * // results is now equals to: {one: 1, two: 2} 86 * }); 87 */ 88 function parallel(tasks, callback) { 89 return (0, _parallel3.default)(_eachOf2.default, tasks, callback); 90 } 91 module.exports = exports['default'];