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