What is run-parallel?
The run-parallel npm package is designed to run multiple functions in parallel, without waiting until the previous function has completed. It is particularly useful for executing asynchronous tasks concurrently, such as I/O operations, in an efficient manner. Once all tasks have completed, a final callback is called with the results of each task.
What are run-parallel's main functionalities?
Running multiple asynchronous tasks in parallel
This feature allows for executing multiple asynchronous tasks concurrently. The code sample demonstrates how to use run-parallel to execute two tasks in parallel, each completing at different times but being processed together.
const runParallel = require('run-parallel')
const tasks = [
function(callback) {
setTimeout(function() {
callback(null, 'one')
}, 200)
},
function(callback) {
setTimeout(function() {
callback(null, 'two')
}, 100)
}
]
runParallel(tasks, function(err, results) {
console.log(results) // ['one', 'two']
})
Other packages similar to run-parallel
async
The async package offers a wide range of functions for working with asynchronous JavaScript, including parallel execution. Compared to run-parallel, async provides a broader set of utilities for handling asynchronous operations, making it more versatile but potentially heavier for projects only needing parallel execution.
p-map
p-map is a promise-based map function designed for running multiple promises in parallel, with support for concurrency limits. Unlike run-parallel, which uses callbacks, p-map utilizes promises, making it a better fit for modern asynchronous patterns in JavaScript.
paralleljs
ParallelJS is a library for parallel programming with JavaScript, allowing for the execution of code in parallel processes. It differs from run-parallel by focusing on parallel computation rather than just asynchronous task execution, potentially offering more power for CPU-bound tasks.
run-parallel
Run an array of functions in parallel
install
npm install run-parallel
usage
parallel(tasks, [callback])
Run the tasks
array of functions in parallel, without waiting until the previous
function has completed. If any of the functions pass an error to its callback, the main
callback
is immediately called with the value of the error. Once the tasks
have
completed, the results are passed to the final callback
as an array.
It is also possible to use an object instead of an array. Each property will be run as a
function and the results will be passed to the final callback
as an object instead of
an array. This can be a more readable way of handling the results.
arguments
tasks
- An array or object containing functions to run. Each function is passed a
callback(err, result)
which it must call on completion with an error err
(which can
be null
) and an optional result
value.callback(err, results)
- An optional callback to run once all the functions have
completed. This function gets a results array (or object) containing all the result
arguments passed to the task callbacks.
example
var parallel = require('run-parallel')
parallel([
function (callback) {
setTimeout(function () {
callback(null, 'one')
}, 200)
},
function (callback) {
setTimeout(function () {
callback(null, 'two')
}, 100)
}
],
function (err, results) {
})
This module is basically equavalent to
async.parallel
, but it's
handy to just have the one function you need instead of the kitchen sink. Modularity!
Especially handy if you're serving to the browser and need to reduce your javascript
bundle size.
Works great in the browser with browserify!
see also
license
MIT. Copyright (c) Feross Aboukhadijeh.