parallel.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = parallelLimit;
  6. var _eachOf = require('./eachOf');
  7. var _eachOf2 = _interopRequireDefault(_eachOf);
  8. var _parallel = require('./internal/parallel');
  9. var _parallel2 = _interopRequireDefault(_parallel);
  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. * It is also possible to use an object instead of an array. Each property will
  25. * be run as a function and the results will be passed to the final `callback`
  26. * as an object instead of an array. This can be a more readable way of handling
  27. * results from {@link async.parallel}.
  28. *
  29. * @name parallel
  30. * @static
  31. * @memberOf module:ControlFlow
  32. * @method
  33. * @category Control Flow
  34. * @param {Array|Iterable|Object} tasks - A collection containing functions to run.
  35. * Each function is passed a `callback(err, result)` which it must call on
  36. * completion with an error `err` (which can be `null`) and an optional `result`
  37. * value.
  38. * @param {Function} [callback] - An optional callback to run once all the
  39. * functions have completed successfully. This function gets a results array
  40. * (or object) containing all the result arguments passed to the task callbacks.
  41. * Invoked with (err, results).
  42. * @example
  43. * async.parallel([
  44. * function(callback) {
  45. * setTimeout(function() {
  46. * callback(null, 'one');
  47. * }, 200);
  48. * },
  49. * function(callback) {
  50. * setTimeout(function() {
  51. * callback(null, 'two');
  52. * }, 100);
  53. * }
  54. * ],
  55. * // optional callback
  56. * function(err, results) {
  57. * // the results array will equal ['one','two'] even though
  58. * // the second function had a shorter timeout.
  59. * });
  60. *
  61. * // an example using an object instead of an array
  62. * async.parallel({
  63. * one: function(callback) {
  64. * setTimeout(function() {
  65. * callback(null, 1);
  66. * }, 200);
  67. * },
  68. * two: function(callback) {
  69. * setTimeout(function() {
  70. * callback(null, 2);
  71. * }, 100);
  72. * }
  73. * }, function(err, results) {
  74. * // results is now equals to: {one: 1, two: 2}
  75. * });
  76. */
  77. function parallelLimit(tasks, callback) {
  78. (0, _parallel2.default)(_eachOf2.default, tasks, callback);
  79. }
  80. module.exports = exports['default'];