Socket
Socket
Sign inDemoInstall

throat

Package Overview
Dependencies
1
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    throat

Throttle the parallelism of an asynchronous (promise returning) function / functions


Version published
Weekly downloads
12M
increased by0.34%
Maintainers
1
Install size
26.3 kB
Created
Weekly downloads
 

Readme

Source

throat

Throttle the parallelism of an asynchronous, promise returning, function / functions. This has special utility when you set the concurrency to 1. That way you get a mutually exclusive lock.

Build Status Dependency Status NPM version

Installation

npm install throat

API

throat(concurrency)

This returns a function that acts a bit like a lock (exactly as a lock if concurrency is 1).

Example, only 2 of the following functions will execute at any one time:

var throat = require('throat')(2)

var resA = throat(function () {
  //async stuff
  return promise
})
var resA = throat(function () {
  //async stuff
  return promise
})
var resA = throat(function () {
  //async stuff
  return promise
})
var resA = throat(function () {
  //async stuff
  return promise
})
var resA = throat(function () {
  //async stuff
  return promise
})

throat(concurrency, worker)

This returns a function that is an exact copy of worker except that it will only execute up to concurrency times in parallel before further requests are queued:

var input = ['fileA.txt', 'fileB.txt', 'fileC.txt', 'fileD.txt']
var data = Promise.all(input.map(throat(2, function (fileName) {
  return readFile(fileName)
})))

Only 2 files will be read at a time, sometimes limiting parallelism in this way can improve scalability.

License

MIT

Keywords

FAQs

Last updated on 27 Jul 2013

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