Skip to content

Call an array of promise-returning functions, restricting concurrency to a specified limit.

License

Notifications You must be signed in to change notification settings

isaacs/promise-call-limit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

promise-call-limit

Call an array of promise-returning functions, restricting concurrency to a specified limit.

USAGE

import { callLimit } from 'promise-call-limit'
// or: const { callLimit } = require('promise-call-limit')

const things = getLongListOfThingsToFrobulate()

// frobulate no more than 4 things in parallel
callLimit(things.map(thing => () => frobulateThing(thing)), {
limit: 4 })
  .then(results => console.log('frobulated 4 at a time', results))

API

callLimit(queue Array<() => Promise>, opts)

opts can contain:

  • limit: specified concurrency limit. Defaults to the number of CPUs on the system minus one. Presumably the main thread is taking up a CPU as well, so let's not be greedy. In the case where there is only one cpu the limit will default to 1.
  • rejectLate: if true, then any rejection will not prevent the rest of the queue from running. Any subsequent rejections will be ignored, and the first rejection will be what the function finally rejects with.

Note that the array should be a list of Promise-returning functions, not Promises themselves. If you have a bunch of Promises already, you're best off just calling Promise.all().

The functions in the queue are called without any arguments.

About

Call an array of promise-returning functions, restricting concurrency to a specified limit.

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages