UNPKG

2.37 kBMarkdownView Raw
1# throat
2
3Throttle 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.
4
5[Professionally supported throat is now available](https://tidelift.com/subscription/pkg/npm-throat?utm_source=npm-throat&utm_medium=referral&utm_campaign=readme)
6
7[![Build Status](https://img.shields.io/github/actions/workflow/status/ForbesLindesay/throat/test.yml?branch=master&style=for-the-badge)](https://github.com/ForbesLindesay/throat/actions/workflows/test.yml?query=branch%3Amaster)
8[![Coveralls github branch](https://img.shields.io/coveralls/github/ForbesLindesay/throat/master?color=brightgreen&style=for-the-badge)](https://coveralls.io/github/ForbesLindesay/throat)
9[![Rolling Versions](https://img.shields.io/badge/Rolling%20Versions-Enabled-brightgreen?style=for-the-badge)](https://rollingversions.com/ForbesLindesay/throat)
10[![NPM version](https://img.shields.io/npm/v/throat?style=for-the-badge)](https://www.npmjs.com/package/throat)
11
12## Installation
13
14 npm install throat
15
16## API
17
18### throat(concurrency)
19
20This returns a function that acts a bit like a lock (exactly as a lock if concurrency is 1).
21
22Example, only 2 of the following functions will execute at any one time:
23
24```js
25const throat = require('throat')(2);
26
27const resA = throat(async () => {
28 /* async stuff... */
29});
30const resB = throat(async () => {
31 /* async stuff... */
32});
33const resC = throat(async () => {
34 /* async stuff... */
35});
36const resD = throat(async () => {
37 /* async stuff... */
38});
39const resE = throat(async () => {
40 /* async stuff... */
41});
42```
43
44### throat(concurrency, worker)
45
46This 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:
47
48```js
49const throat = require('throat');
50
51const input = ['fileA.txt', 'fileB.txt', 'fileC.txt', 'fileD.txt'];
52const data = Promise.all(
53 input.map(throat(2, (fileName) => readFile(fileName)))
54);
55```
56
57Only 2 files will be read at a time, sometimes limiting parallelism in this way can improve scalability.
58
59## Security contact information
60
61To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.
62
63## License
64
65MIT