or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-run-parallel

Run an array of functions in parallel

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/run-parallel@1.2.x

To install, run

npx @tessl/cli install tessl/npm-run-parallel@1.2.0

index.mddocs/

Run Parallel

Run 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.

Package Information

  • Package Name: run-parallel
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install run-parallel

Core Imports

const parallel = require('run-parallel');

or

const runParallel = require('run-parallel');

Basic Usage

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'] }
});

Capabilities

Parallel Execution

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);

Parameters

  • tasks (Array|Object): Collection of functions to execute in parallel
    • If Array: Functions are executed in parallel, results returned as array preserving order
    • If Object: Functions are executed in parallel, results returned as object with same keys
    • Each task function receives a callback with signature callback(err, result)
  • callback (Function, optional): Completion callback with signature callback(err, results)
    • Called when all tasks complete successfully or when first error occurs
    • err: Error from first failed task, or null if all succeeded
    • results: Array or Object matching input type containing all results

Behavior

  • Parallel Execution: All tasks start immediately, don't wait for previous tasks
  • Order Preservation: For arrays, results maintain the same order as input tasks
  • Error Handling: Stops on first error and calls main callback immediately
  • Empty Input: Calls callback immediately with empty results for empty arrays/objects
  • Async Consistency: Uses microtask queuing for consistent asynchronous behavior
  • Optional Callback: Tasks still execute even if no callback is provided

Usage Examples

Array 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"

Browser Compatibility

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.

Dependencies

  • queue-microtask: ^1.2.2 - Provides consistent microtask scheduling across environments