Skip to content
Throttle a collection of promise returning functions
JavaScript TypeScript
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
test fix: allow exra args and use default export in TypeScript May 24, 2019
.flowconfig Add definitions for typescript and flow (#19) Jun 13, 2017
.gitignore Add definitions for typescript and flow (#19) Jun 13, 2017
.prettierrc Update flow-bin to the latest version 🚀 (#39) Mar 12, 2018
.travis.yml test: run browser tests on the latest version of node and stop testin… Apr 12, 2019
LICENSE New API Design Jul 27, 2013
README.md test: run browser tests on the latest version of node and stop testin… Apr 12, 2019
SECURITY.md chore: add security policy May 30, 2019
index.d.ts fix: allow exra args and use default export in TypeScript May 24, 2019
index.js fix: allow exra args and use default export in TypeScript May 24, 2019
index.js.flow Add definitions for typescript and flow (#19) Jun 13, 2017
package.json Release 5.0.0 May 24, 2019
perf.js Use an efficient queue implementation (#15) Jun 4, 2017
tsconfig.json

README.md

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 Coverage Status Dependency Status NPM version Greenkeeper badge

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);
// alternatively provide your own promise implementation
const throat = require('throat')(require('promise'))(2);
const promise = Promise.resolve();

const resA = throat(() => /* async stuff... */ promise);
const resB = throat(() => /* async stuff... */ promise);
const resC = throat(() => /* async stuff... */ promise);
const resD = throat(() => /* async stuff... */ promise);
const resE = throat(() => /* async stuff... */ 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:

const throat = require('throat');
// alternatively provide your own promise implementation
const throat = require('throat')(require('promise'));

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

You can’t perform that action at this time.