parallel.js

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