Run an array of functions in parallel
npx @tessl/cli install tessl/npm-run-parallel@1.2.0Run Parallel is a lightweight utility that executes an array or object of asynchronous functions in parallel without waiting for each function to complete before starting the next. It serves as a modular alternative to async.parallel, focusing on a single core functionality to reduce bundle size for browser applications.
npm install run-parallelconst parallel = require('run-parallel');or
const runParallel = require('run-parallel');const parallel = require('run-parallel');
// Array of tasks - results returned as array
parallel([
function (callback) {
setTimeout(() => callback(null, 'task1'), 100);
},
function (callback) {
setTimeout(() => callback(null, 'task2'), 50);
}
], function (err, results) {
if (err) throw err;
console.log(results); // ['task1', 'task2'] - order preserved
});
// Object of tasks - results returned as object
parallel({
users: function (callback) {
// Fetch users
callback(null, ['alice', 'bob']);
},
posts: function (callback) {
// Fetch posts
callback(null, ['post1', 'post2']);
}
}, function (err, results) {
if (err) throw err;
console.log(results); // { users: ['alice', 'bob'], posts: ['post1', 'post2'] }
});Executes an array or object of asynchronous functions in parallel and collects their results.
/**
* Run an array or object of functions in parallel
* @param {Array|Object} tasks - Collection of functions to execute in parallel
* @param {Function} [callback] - Optional completion callback
* @returns {undefined} - Does not return a value; results are provided via callback
*/
function runParallel(tasks, callback);callback(err, result)callback(err, results)
err: Error from first failed task, or null if all succeededresults: Array or Object matching input type containing all resultsArray of Tasks:
const parallel = require('run-parallel');
parallel([
function (callback) {
fs.readFile('file1.txt', callback);
},
function (callback) {
fs.readFile('file2.txt', callback);
},
function (callback) {
fs.readFile('file3.txt', callback);
}
], function (err, results) {
if (err) {
console.error('Error reading files:', err);
return;
}
// results[0] = contents of file1.txt
// results[1] = contents of file2.txt
// results[2] = contents of file3.txt
console.log('All files read successfully');
});Object of Tasks:
const parallel = require('run-parallel');
parallel({
weather: function (callback) {
fetchWeather((err, data) => callback(err, data));
},
news: function (callback) {
fetchNews((err, data) => callback(err, data));
},
stocks: function (callback) {
fetchStocks((err, data) => callback(err, data));
}
}, function (err, results) {
if (err) {
console.error('Error fetching data:', err);
return;
}
// results.weather = weather data
// results.news = news data
// results.stocks = stock data
console.log('All data fetched:', results);
});Error Handling:
const parallel = require('run-parallel');
parallel([
function (callback) {
callback(null, 'success');
},
function (callback) {
callback(new Error('Something went wrong'));
},
function (callback) {
// This might not complete if error occurs first
setTimeout(() => callback(null, 'delayed'), 1000);
}
], function (err, results) {
if (err) {
console.error('Task failed:', err.message); // "Something went wrong"
// results may contain partial data: ['success', undefined, undefined]
return;
}
console.log('All tasks completed:', results);
});Without Callback:
const parallel = require('run-parallel');
// Tasks still execute, results are discarded
parallel([
function (callback) {
console.log('Task 1 executing');
callback(null, 'done');
},
function (callback) {
console.log('Task 2 executing');
callback(null, 'done');
}
]);
// Output: "Task 1 executing" and "Task 2 executing"This package works in browsers when bundled with browserify or similar tools. It uses the queue-microtask dependency to ensure consistent asynchronous behavior across different JavaScript environments.