Skip to content

Run an array of functions in parallel, wait to finish when one errors.

License

Notifications You must be signed in to change notification settings

vweevers/run-parallel-settled

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

run-parallel-settled

Run an array of functions in parallel, wait to finish when one errors. Similar to run-parallel and run-parallel-limit which return as soon as the first function errors, while this waits for functions that already started. Useful to ensure that once the final callback is called, any resources created by the functions have been cleaned up.

npm status node Travis build status JavaScript Style Guide

Usage

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

const tasks = [
  function one (next) {
    next(new Error('failed'))
  },
  function two (next) {
    setTimeout(function () {
      console.log('wait for me')
      next(null, 123)
    }, 1e3)
  },
  function three (next) {
    next(new Error('also failed'))
  }
]

parallel(tasks, 3, function (err, results) {
  if (err) throw err
})

This will wait for function two, then throw a combined error from one and three:

$ node example.js
wait for me
/examples/example.js:19
  if (err) throw err
           ^

Error: failed
    at Array.one (/examples/example.js:5:10)
    at parallel (/examples/node_modules/run-parallel-settled/index.js:27:13)
    at ..

Error: also failed
    at Array.three (/examples/example.js:14:10)
    at parallel (/examples/node_modules/run-parallel-settled/index.js:27:13)
    at ..

API

parallel(tasks[, limit], callback)

Run the functions in the tasks array, with a maximum of limit functions executing in parallel. The limit argument defaults to Infinity i.e. no limit. The callback function with signature callback(err, results) will be called once all functions have finished without error, or once one of them errors and all functions that were started have finished.

Install

With npm do:

npm install run-parallel-settled

License

MIT © 2020-present Vincent Weevers

About

Run an array of functions in parallel, wait to finish when one errors.

Topics

Resources

License

Stars

Watchers

Forks