1 | # throat
|
2 |
|
3 | 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.
|
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/workflow/status/ForbesLindesay/throat/Test/master?style=for-the-badge)](https://github.com/ForbesLindesay/throat/actions?query=workflow%3ATest+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 |
|
20 | This returns a function that acts a bit like a lock (exactly as a lock if concurrency is 1).
|
21 |
|
22 | Example, only 2 of the following functions will execute at any one time:
|
23 |
|
24 | ```js
|
25 | const throat = require('throat')(2);
|
26 |
|
27 | const resA = throat(async () => { /* async stuff... */ });
|
28 | const resB = throat(async () => { /* async stuff... */ });
|
29 | const resC = throat(async () => { /* async stuff... */ });
|
30 | const resD = throat(async () => { /* async stuff... */ });
|
31 | const resE = throat(async () => { /* async stuff... */ });
|
32 | ```
|
33 |
|
34 | ### throat(concurrency, worker)
|
35 |
|
36 | 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:
|
37 |
|
38 | ```js
|
39 | const throat = require('throat');
|
40 |
|
41 | const input = ['fileA.txt', 'fileB.txt', 'fileC.txt', 'fileD.txt'];
|
42 | const data = Promise.all(
|
43 | input.map(throat(2, (fileName) => readFile(fileName)))
|
44 | );
|
45 | ```
|
46 |
|
47 | Only 2 files will be read at a time, sometimes limiting parallelism in this way can improve scalability.
|
48 |
|
49 | ## Security contact information
|
50 |
|
51 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.
|
52 |
|
53 | ## License
|
54 |
|
55 | MIT
|