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