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
npm install throat
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... */ });
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.
To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.
MIT