You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

throat

Package Overview
Dependencies
Maintainers
2
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

throat

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


Version published
Maintainers
2
Created

Package description

What is throat?

The 'throat' npm package is a utility for limiting the number of concurrent asynchronous operations. It is particularly useful when you need to control the rate of operations to avoid overwhelming resources or hitting rate limits.

What are throat's main functionalities?

Limit Concurrent Promises

This feature allows you to limit the number of concurrent promises. In this example, only 2 tasks will run concurrently.

const throat = require('throat')(2);

const tasks = [
  () => new Promise(resolve => setTimeout(() => resolve('Task 1'), 1000)),
  () => new Promise(resolve => setTimeout(() => resolve('Task 2'), 500)),
  () => new Promise(resolve => setTimeout(() => resolve('Task 3'), 300)),
  () => new Promise(resolve => setTimeout(() => resolve('Task 4'), 200))
];

Promise.all(tasks.map(throat(task => task()))).then(results => {
  console.log(results); // ['Task 1', 'Task 2', 'Task 3', 'Task 4']
});

Limit Concurrent Function Calls

This feature allows you to limit the number of concurrent function calls. In this example, only 1 task will run at a time.

const throat = require('throat')(1);

const task = (name, delay) => () => new Promise(resolve => setTimeout(() => resolve(name), delay));

const tasks = [
  task('Task 1', 1000),
  task('Task 2', 500),
  task('Task 3', 300),
  task('Task 4', 200)
];

Promise.all(tasks.map(throat(task => task()))).then(results => {
  console.log(results); // ['Task 1', 'Task 2', 'Task 3', 'Task 4']
});

Other packages similar to throat

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.

Professionally supported throat is now available

Build Status Coveralls github branch Rolling Versions 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:

const throat = require('throat')(2);

const resA = throat(async () => {
  /* async stuff... */
});
const resB = throat(async () => {
  /* async stuff... */
});
const resC = throat(async () => {
  /* async stuff... */
});
const resD = throat(async () => {
  /* async stuff... */
});
const resE = throat(async () => {
  /* async stuff... */
});

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:

const throat = require('throat');

const input = ['fileA.txt', 'fileB.txt', 'fileC.txt', 'fileD.txt'];
const data = Promise.all(
  input.map(throat(2, (fileName) => readFile(fileName)))
);

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

Security contact information

To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.

License

MIT

Keywords

FAQs

Package last updated on 03 Jan 2023

Did you know?

Socket

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
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc