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

concurrent-control

v1.0.1

Published

Controls the number of simultaneous calls to a promise-returning function

Downloads

2

Readme

Description

Controls the number of simultaneous calls to a promise-returning function.

const cc = require('concurrent-control');

const controller = cc();

function Test(value) {
  console.info("Test",value,"enters");
  return new Promise((resolve,reject)=>{
    setTimeout(()=>{
      console.info("Test",value,"returns");
      resolve();
    },5000);
  });
}

function TestCaller(value) {
  controller(()=>{
    return Test(value);
  });
}

TestCaller(1);
TestCaller(2);

this produces:

Test 1 enters
Test 1 returns
Test 2 enters
Test 2 returns

Install

npm install concurrent-control --save

Documentation

The module exports a function to be called to create a controller.

// const concurrentControl = require('concurrent-control');
// var controller =

moduleFunction([options])

options: an integer representing the number of allowed simultaneous calls, or a function returning this number, or a function returning a Promise that resolves to this number. If options is omitted, the default value is 1, meaning the controlled code can be called once at a time.

Returns a function to be called every time the controlled code is to be invoked:

controller(fn[,unqueueFn])

fn: the function to be called in a controlled way

unqueueFn: callback function called right after the call has been queued and offers functions as parameters to remove the call from the queue, resulting in resolving or reject the controller Promise. Note that if the call has started or is being executed, it cannot be aborted this way.

Returns a Promise that will either resolve or reject as the result of the code being executed or un-queued.

Example:

function CallCode(param) {
  console.info("CallCode",param);
  controller(()=>{
    console.info("Code",param,"starting");
    return codeThatTakes3SecondsToResolve()
      .then(()=>{
        console.info("Code",param,"done");
      });
  },(resolve,reject)=>{
    console.info("Got unqueue handlers for",param);
    setTimeout(()=>{
      console.info("Aborting",param);
      reject(); // resolve() would also un-queue the call
    },5000);
  });
}
for(var i=1; i<=3; i++)
  (function(index) {
    CallCode(index)
      .then(function() {
        console.info("Call",index,"resolved");
      })
      .catch(function(err) {
        console.info("Call",index,"rejected");
      })
    })(i);

produces:

CallCode 1
CallCode 2
CallCode 3
Code 1 starting
Got abort handler for 2
Got abort handler for 3
Code 1 executed
Code 2 starting
Call 1 resolved
Aborting 2
Aborting 3
Call 3 resolved
Code 2 executed
Call 2 resolved

unqueueFn for call 1 is never invoked since the action starts immediately. When the un-queue reject function for call 2 is triggered, this call has already started so this has no effect. Call 3 never starts since it was un-queued before it started executing.