make-concurrent
v5.4.0
Published
Easy exclusive or shared access to resource
Downloads
30,729
Readme
make-concurrent
- What this is it?
- Installation
- Where make-concurrent can be used?
- How make-concurrent work?
- Examples
- LICENSE
What this is it?
make-concurrent
is a function which create function with limited parallel execution of passed function according with passed concurrency
option (1
by default).
It's not replacement of lodash.debounce / lodash.throttle. Created function called immediately if current executed functions not excess concurrency
option, otherwise call will be blocked until previous called functions not finished.
make-concurrent
functions does not make any sense for synchronous functions, because JS can not execute more than 1 same synchronous function at one time, it's can be useful only for async functions.
Callbacks are not supported, async functions with us from Node v7.6.0 (2017-02-22), please use it.
Installation
npm:
npm install https://github.com/fanatid/make-concurrent
yarn:
yarn add https://github.com/fanatid/make-concurrent
By default npm
/ yarn
will install code from master
branch. If you want specified version, just add some branch name / commit hash / tag and the end of URL. See Yarn add or npm install for details about installing package from git repo.
Where make-concurrent can be used?
Anywhere where you need limited access to some resource.
How make-concurrent work?
When we call function created by make-concurrent
counter of called functions increased by 1
, then increased counter compared with concurrency
option, if counter is greater than concurrency
then we create Promise
, push resolve
function to queue (FIFO) and wait while this Promise
will be resolved, before call original function. When execution of original function will be finished we decrease counter on 1
and check queue, if queue length is not zero we take first item and call it, which resolve Promise
and original function will be called again.
All code is less than 50 lines, just check it. This will take 2 minutes, but will give better understanding how make-concurrent
works and how can be used effectively.
Examples
Limited access by API key
Assume you need make requests to some API, but this API have limit of simultaneous requests by API key.
const makeConcurrent = require('make-concurrent')
const fetch = require('node-fetch')
async function request (user) {
return fetch(`https://example.org/getinfo?user=${user}&apikey=${process.env.API_KEY}`)
}
module.exports = makeConcurrent(request, { concurrency: 3 })
Now only 3 request
function will work at one moment.
Safe work with shared state
Sometimes we have state and need work with it from async functions which can be executed at one time.
const makeConcurrent = require('make-concurrent')
const state = {}
async function unsafeUpdate (user) {
const currentValue = state[user] === undefined ? 0 : state[user]
const newValue = await getNewValue(user, currentValue)
state[user] = newValue
}
const safeUpdate = makeConcurrent(unsafeChange)
// safeUpdate('alice')
While safeUpdate
going to be safe function for working with state
, it's good as general approach. But here state changed for specified user, so we can use the .byArguments()
method:
const makeConcurrent = require('make-concurrent')
const state = {}
async function unsafeUpdate (user) {
const currentValue = state[user] === undefined ? 0 : state[user]
const newValue = await getNewValue(user, currentValue)
state[user] = newValue
}
const safeUpdate = makeConcurrent.byArguments(unsafeUpdate)
// safeUpdate('alice')