npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

make-concurrent

v5.4.0

Published

Easy exclusive or shared access to resource

Downloads

30,729

Readme

make-concurrent

Build Status

js-standard-style

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

LICENSE MIT