| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.default = parallel;
|
|---|
| 7 |
|
|---|
| 8 | var _eachOf = require('./eachOf.js');
|
|---|
| 9 |
|
|---|
| 10 | var _eachOf2 = _interopRequireDefault(_eachOf);
|
|---|
| 11 |
|
|---|
| 12 | var _parallel2 = require('./internal/parallel.js');
|
|---|
| 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 | *
|
|---|
| 55 | * //Using Callbacks
|
|---|
| 56 | * async.parallel([
|
|---|
| 57 | * function(callback) {
|
|---|
| 58 | * setTimeout(function() {
|
|---|
| 59 | * callback(null, 'one');
|
|---|
| 60 | * }, 200);
|
|---|
| 61 | * },
|
|---|
| 62 | * function(callback) {
|
|---|
| 63 | * setTimeout(function() {
|
|---|
| 64 | * callback(null, 'two');
|
|---|
| 65 | * }, 100);
|
|---|
| 66 | * }
|
|---|
| 67 | * ], function(err, results) {
|
|---|
| 68 | * console.log(results);
|
|---|
| 69 | * // results is equal to ['one','two'] even though
|
|---|
| 70 | * // the second function had a shorter timeout.
|
|---|
| 71 | * });
|
|---|
| 72 | *
|
|---|
| 73 | * // an example using an object instead of an array
|
|---|
| 74 | * async.parallel({
|
|---|
| 75 | * one: function(callback) {
|
|---|
| 76 | * setTimeout(function() {
|
|---|
| 77 | * callback(null, 1);
|
|---|
| 78 | * }, 200);
|
|---|
| 79 | * },
|
|---|
| 80 | * two: function(callback) {
|
|---|
| 81 | * setTimeout(function() {
|
|---|
| 82 | * callback(null, 2);
|
|---|
| 83 | * }, 100);
|
|---|
| 84 | * }
|
|---|
| 85 | * }, function(err, results) {
|
|---|
| 86 | * console.log(results);
|
|---|
| 87 | * // results is equal to: { one: 1, two: 2 }
|
|---|
| 88 | * });
|
|---|
| 89 | *
|
|---|
| 90 | * //Using Promises
|
|---|
| 91 | * async.parallel([
|
|---|
| 92 | * function(callback) {
|
|---|
| 93 | * setTimeout(function() {
|
|---|
| 94 | * callback(null, 'one');
|
|---|
| 95 | * }, 200);
|
|---|
| 96 | * },
|
|---|
| 97 | * function(callback) {
|
|---|
| 98 | * setTimeout(function() {
|
|---|
| 99 | * callback(null, 'two');
|
|---|
| 100 | * }, 100);
|
|---|
| 101 | * }
|
|---|
| 102 | * ]).then(results => {
|
|---|
| 103 | * console.log(results);
|
|---|
| 104 | * // results is equal to ['one','two'] even though
|
|---|
| 105 | * // the second function had a shorter timeout.
|
|---|
| 106 | * }).catch(err => {
|
|---|
| 107 | * console.log(err);
|
|---|
| 108 | * });
|
|---|
| 109 | *
|
|---|
| 110 | * // an example using an object instead of an array
|
|---|
| 111 | * async.parallel({
|
|---|
| 112 | * one: function(callback) {
|
|---|
| 113 | * setTimeout(function() {
|
|---|
| 114 | * callback(null, 1);
|
|---|
| 115 | * }, 200);
|
|---|
| 116 | * },
|
|---|
| 117 | * two: function(callback) {
|
|---|
| 118 | * setTimeout(function() {
|
|---|
| 119 | * callback(null, 2);
|
|---|
| 120 | * }, 100);
|
|---|
| 121 | * }
|
|---|
| 122 | * }).then(results => {
|
|---|
| 123 | * console.log(results);
|
|---|
| 124 | * // results is equal to: { one: 1, two: 2 }
|
|---|
| 125 | * }).catch(err => {
|
|---|
| 126 | * console.log(err);
|
|---|
| 127 | * });
|
|---|
| 128 | *
|
|---|
| 129 | * //Using async/await
|
|---|
| 130 | * async () => {
|
|---|
| 131 | * try {
|
|---|
| 132 | * let results = await async.parallel([
|
|---|
| 133 | * function(callback) {
|
|---|
| 134 | * setTimeout(function() {
|
|---|
| 135 | * callback(null, 'one');
|
|---|
| 136 | * }, 200);
|
|---|
| 137 | * },
|
|---|
| 138 | * function(callback) {
|
|---|
| 139 | * setTimeout(function() {
|
|---|
| 140 | * callback(null, 'two');
|
|---|
| 141 | * }, 100);
|
|---|
| 142 | * }
|
|---|
| 143 | * ]);
|
|---|
| 144 | * console.log(results);
|
|---|
| 145 | * // results is equal to ['one','two'] even though
|
|---|
| 146 | * // the second function had a shorter timeout.
|
|---|
| 147 | * }
|
|---|
| 148 | * catch (err) {
|
|---|
| 149 | * console.log(err);
|
|---|
| 150 | * }
|
|---|
| 151 | * }
|
|---|
| 152 | *
|
|---|
| 153 | * // an example using an object instead of an array
|
|---|
| 154 | * async () => {
|
|---|
| 155 | * try {
|
|---|
| 156 | * let results = await async.parallel({
|
|---|
| 157 | * one: function(callback) {
|
|---|
| 158 | * setTimeout(function() {
|
|---|
| 159 | * callback(null, 1);
|
|---|
| 160 | * }, 200);
|
|---|
| 161 | * },
|
|---|
| 162 | * two: function(callback) {
|
|---|
| 163 | * setTimeout(function() {
|
|---|
| 164 | * callback(null, 2);
|
|---|
| 165 | * }, 100);
|
|---|
| 166 | * }
|
|---|
| 167 | * });
|
|---|
| 168 | * console.log(results);
|
|---|
| 169 | * // results is equal to: { one: 1, two: 2 }
|
|---|
| 170 | * }
|
|---|
| 171 | * catch (err) {
|
|---|
| 172 | * console.log(err);
|
|---|
| 173 | * }
|
|---|
| 174 | * }
|
|---|
| 175 | *
|
|---|
| 176 | */
|
|---|
| 177 | function parallel(tasks, callback) {
|
|---|
| 178 | return (0, _parallel3.default)(_eachOf2.default, tasks, callback);
|
|---|
| 179 | }
|
|---|
| 180 | module.exports = exports.default; |
|---|