throat
Throttle the parallelism of an asynchronous (promise returning) function / functions
Last updated 8 years ago by forbeslindesay .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install throat 
SYNC missed versions from official npm registry.

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

Current Tags

  • 6.0.2                                ...           latest (2 years ago)

14 Versions

  • 6.0.2                                ...           2 years ago
  • 6.0.1                                ...           4 years ago
  • 6.0.0                                ...           4 years ago
  • 5.0.0                                ...           6 years ago
  • 4.1.0                                ...           7 years ago
  • 3.1.0                                ...           8 years ago
  • 4.0.0                                ...           7 years ago
  • 3.2.0                                ...           7 years ago
  • 3.0.0                                ...           9 years ago
  • 2.0.2                                ...           10 years ago
  • 2.0.1                                ...           10 years ago
  • 2.0.0                                ...           10 years ago
  • 1.0.0                                ...           11 years ago
  • 0.0.0                                ...           12 years ago
Downloads
Today 0
This Week 0
This Month 3
Last Day 0
Last Week 3
Last Month 0
Dependencies (1)
Dev Dependencies (6)

Copyright 2013 - present © cnpmjs.org | Home |