parallel.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = parallel;
  6. var _eachOf = require('./eachOf.js');
  7. var _eachOf2 = _interopRequireDefault(_eachOf);
  8. var _parallel2 = require('./internal/parallel.js');
  9. var _parallel3 = _interopRequireDefault(_parallel2);
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. /**
  12. * Run the `tasks` collection of functions in parallel, without waiting until
  13. * the previous function has completed. If any of the functions pass an error to
  14. * its callback, the main `callback` is immediately called with the value of the
  15. * error. Once the `tasks` have completed, the results are passed to the final
  16. * `callback` as an array.
  17. *
  18. * **Note:** `parallel` is about kicking-off I/O tasks in parallel, not about
  19. * parallel execution of code. If your tasks do not use any timers or perform
  20. * any I/O, they will actually be executed in series. Any synchronous setup
  21. * sections for each task will happen one after the other. JavaScript remains
  22. * single-threaded.
  23. *
  24. * **Hint:** Use [`reflect`]{@link module:Utils.reflect} to continue the
  25. * execution of other tasks when a task fails.
  26. *
  27. * It is also possible to use an object instead of an array. Each property will
  28. * be run as a function and the results will be passed to the final `callback`
  29. * as an object instead of an array. This can be a more readable way of handling
  30. * results from {@link async.parallel}.
  31. *
  32. * @name parallel
  33. * @static
  34. * @memberOf module:ControlFlow
  35. * @method
  36. * @category Control Flow
  37. * @param {Array|Iterable|AsyncIterable|Object} tasks - A collection of
  38. * [async functions]{@link AsyncFunction} to run.
  39. * Each async function can complete with any number of optional `result` values.
  40. * @param {Function} [callback] - An optional callback to run once all the
  41. * functions have completed successfully. This function gets a results array
  42. * (or object) containing all the result arguments passed to the task callbacks.
  43. * Invoked with (err, results).
  44. * @returns {Promise} a promise, if a callback is not passed
  45. *
  46. * @example
  47. *
  48. * //Using Callbacks
  49. * async.parallel([
  50. * function(callback) {
  51. * setTimeout(function() {
  52. * callback(null, 'one');
  53. * }, 200);
  54. * },
  55. * function(callback) {
  56. * setTimeout(function() {
  57. * callback(null, 'two');
  58. * }, 100);
  59. * }
  60. * ], function(err, results) {
  61. * console.log(results);
  62. * // results is equal to ['one','two'] even though
  63. * // the second function had a shorter timeout.
  64. * });
  65. *
  66. * // an example using an object instead of an array
  67. * async.parallel({
  68. * one: function(callback) {
  69. * setTimeout(function() {
  70. * callback(null, 1);
  71. * }, 200);
  72. * },
  73. * two: function(callback) {
  74. * setTimeout(function() {
  75. * callback(null, 2);
  76. * }, 100);
  77. * }
  78. * }, function(err, results) {
  79. * console.log(results);
  80. * // results is equal to: { one: 1, two: 2 }
  81. * });
  82. *
  83. * //Using Promises
  84. * async.parallel([
  85. * function(callback) {
  86. * setTimeout(function() {
  87. * callback(null, 'one');
  88. * }, 200);
  89. * },
  90. * function(callback) {
  91. * setTimeout(function() {
  92. * callback(null, 'two');
  93. * }, 100);
  94. * }
  95. * ]).then(results => {
  96. * console.log(results);
  97. * // results is equal to ['one','two'] even though
  98. * // the second function had a shorter timeout.
  99. * }).catch(err => {
  100. * console.log(err);
  101. * });
  102. *
  103. * // an example using an object instead of an array
  104. * async.parallel({
  105. * one: function(callback) {
  106. * setTimeout(function() {
  107. * callback(null, 1);
  108. * }, 200);
  109. * },
  110. * two: function(callback) {
  111. * setTimeout(function() {
  112. * callback(null, 2);
  113. * }, 100);
  114. * }
  115. * }).then(results => {
  116. * console.log(results);
  117. * // results is equal to: { one: 1, two: 2 }
  118. * }).catch(err => {
  119. * console.log(err);
  120. * });
  121. *
  122. * //Using async/await
  123. * async () => {
  124. * try {
  125. * let results = await async.parallel([
  126. * function(callback) {
  127. * setTimeout(function() {
  128. * callback(null, 'one');
  129. * }, 200);
  130. * },
  131. * function(callback) {
  132. * setTimeout(function() {
  133. * callback(null, 'two');
  134. * }, 100);
  135. * }
  136. * ]);
  137. * console.log(results);
  138. * // results is equal to ['one','two'] even though
  139. * // the second function had a shorter timeout.
  140. * }
  141. * catch (err) {
  142. * console.log(err);
  143. * }
  144. * }
  145. *
  146. * // an example using an object instead of an array
  147. * async () => {
  148. * try {
  149. * let results = await async.parallel({
  150. * one: function(callback) {
  151. * setTimeout(function() {
  152. * callback(null, 1);
  153. * }, 200);
  154. * },
  155. * two: function(callback) {
  156. * setTimeout(function() {
  157. * callback(null, 2);
  158. * }, 100);
  159. * }
  160. * });
  161. * console.log(results);
  162. * // results is equal to: { one: 1, two: 2 }
  163. * }
  164. * catch (err) {
  165. * console.log(err);
  166. * }
  167. * }
  168. *
  169. */
  170. function parallel(tasks, callback) {
  171. return (0, _parallel3.default)(_eachOf2.default, tasks, callback);
  172. }
  173. module.exports = exports['default'];