Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ForbesLindesay/throat
Throttle a collection of promise returning functions
https://github.com/ForbesLindesay/throat
concurrency flow promise typescript
Last synced: 15 days ago
JSON representation
Throttle a collection of promise returning functions
- Host: GitHub
- URL: https://github.com/ForbesLindesay/throat
- Owner: ForbesLindesay
- License: mit
- Created: 2012-12-21T10:58:14.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2023-01-03T13:31:48.000Z (almost 2 years ago)
- Last Synced: 2024-10-19T13:57:14.743Z (23 days ago)
- Topics: concurrency, flow, promise, typescript
- Language: JavaScript
- Size: 85 KB
- Stars: 484
- Watchers: 4
- Forks: 40
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
- stars - ForbesLindesay/throat
README
# 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](https://tidelift.com/subscription/pkg/npm-throat?utm_source=npm-throat&utm_medium=referral&utm_campaign=readme)
[![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)
[![Coveralls github branch](https://img.shields.io/coveralls/github/ForbesLindesay/throat/master?color=brightgreen&style=for-the-badge)](https://coveralls.io/github/ForbesLindesay/throat)
[![Rolling Versions](https://img.shields.io/badge/Rolling%20Versions-Enabled-brightgreen?style=for-the-badge)](https://rollingversions.com/ForbesLindesay/throat)
[![NPM version](https://img.shields.io/npm/v/throat?style=for-the-badge)](https://www.npmjs.com/package/throat)## 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:
```js
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:
```js
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](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.
## License
MIT