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