You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
3.1 KiB

2 years ago
  1. # run-parallel [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url]
  2. [travis-image]: https://img.shields.io/travis/feross/run-parallel/master.svg
  3. [travis-url]: https://travis-ci.org/feross/run-parallel
  4. [npm-image]: https://img.shields.io/npm/v/run-parallel.svg
  5. [npm-url]: https://npmjs.org/package/run-parallel
  6. [downloads-image]: https://img.shields.io/npm/dm/run-parallel.svg
  7. [downloads-url]: https://npmjs.org/package/run-parallel
  8. [standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg
  9. [standard-url]: https://standardjs.com
  10. ### Run an array of functions in parallel
  11. ![parallel](https://raw.githubusercontent.com/feross/run-parallel/master/img.png) [![Sauce Test Status](https://saucelabs.com/browser-matrix/run-parallel.svg)](https://saucelabs.com/u/run-parallel)
  12. ### install
  13. ```
  14. npm install run-parallel
  15. ```
  16. ### usage
  17. #### parallel(tasks, [callback])
  18. Run the `tasks` array of functions in parallel, without waiting until the previous
  19. function has completed. If any of the functions pass an error to its callback, the main
  20. `callback` is immediately called with the value of the error. Once the `tasks` have
  21. completed, the results are passed to the final `callback` as an array.
  22. It is also possible to use an object instead of an array. Each property will be run as a
  23. function and the results will be passed to the final `callback` as an object instead of
  24. an array. This can be a more readable way of handling the results.
  25. ##### arguments
  26. - `tasks` - An array or object containing functions to run. Each function is passed a
  27. `callback(err, result)` which it must call on completion with an error `err` (which can
  28. be `null`) and an optional `result` value.
  29. - `callback(err, results)` - An optional callback to run once all the functions have
  30. completed. This function gets a results array (or object) containing all the result
  31. arguments passed to the task callbacks.
  32. ##### example
  33. ```js
  34. var parallel = require('run-parallel')
  35. parallel([
  36. function (callback) {
  37. setTimeout(function () {
  38. callback(null, 'one')
  39. }, 200)
  40. },
  41. function (callback) {
  42. setTimeout(function () {
  43. callback(null, 'two')
  44. }, 100)
  45. }
  46. ],
  47. // optional callback
  48. function (err, results) {
  49. // the results array will equal ['one','two'] even though
  50. // the second function had a shorter timeout.
  51. })
  52. ```
  53. This module is basically equavalent to
  54. [`async.parallel`](https://github.com/caolan/async#paralleltasks-callback), but it's
  55. handy to just have the one function you need instead of the kitchen sink. Modularity!
  56. Especially handy if you're serving to the browser and need to reduce your javascript
  57. bundle size.
  58. Works great in the browser with [browserify](http://browserify.org/)!
  59. ### see also
  60. - [run-auto](https://github.com/feross/run-auto)
  61. - [run-parallel-limit](https://github.com/feross/run-parallel-limit)
  62. - [run-series](https://github.com/feross/run-series)
  63. - [run-waterfall](https://github.com/feross/run-waterfall)
  64. ### license
  65. MIT. Copyright (c) [Feross Aboukhadijeh](http://feross.org).