Skip to content

An utility that provides easy concurrency control of any promise based function

Notifications You must be signed in to change notification settings

bernawil/concurrency-control

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Concurrency Control

Make any function that returns a promise a concurrency controlled function. Useful for dealing with rate limiting.

Installing

$ npm install concurrency-control

Example

Say you need to query an API that implments rate limiting and you have a function that queries it. You could control how many times you execute this function in every place in your project, but it would be better to make it manage its own concurrency and export this controlled function from a module.

const { getFromApi } = require('myApi')
const { makeControlled } = require('concurrency-control')

// a concurrency parameter of 1 makes the function secuential
const MAX_CONCURRENT = 3
const controlledGetFromApi = makeControlled(getFromApi, { concurrency: MAX_CONCURRENT })

let i = 50
while (i--) // functions will be executed in batches, never more than 3 at a time
  controlledGetFromApi(i)
    .then(result => { ... })
    .catch(error => { ... })

About

An utility that provides easy concurrency control of any promise based function

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published