Socket
Socket
Sign inDemoInstall

blocking-promise-chain

Package Overview
Dependencies
0
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    blocking-promise-chain

blocking-promise-chain


Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

blocking-promise-chain

A chain of promises that blocks when a limit is reached. This is useful in an async function to keep an outstanding number of promises running (to keep target busy). See below for examples:

Usage

Say you have a loop that's doing some external IO:

async function worker() {
  for (const data of allMyData) {
    await request.post(serverUrl, data);
  }
}

Nothing wrong here, except that the next post starts only when previous post is finished. To keep server fully busy, we should maintain a queue of outstanding requests. Using this module, code becomes:

import {BlockingPromiseChain} from 'blocking-promise-chain';
const MaxQueuedRequest = 10;

async function worker() {
  let chain = new BlockingPromiseChain(MaxQueuedRequest);
  for (const data of allMyData) {
    await chain.add(request.post(serverUrl, data));
  }
  await chain.flush();    // ensure all outstanding requests are finished
}

Simple as that.

Keywords

FAQs

Last updated on 14 Sep 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc