Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fabiospampinato/promise-concurrency-limiter
Tiny scheduler for functions returning promises that can limit their concurrency.
https://github.com/fabiospampinato/promise-concurrency-limiter
concurrency limit limiter promise
Last synced: about 1 month ago
JSON representation
Tiny scheduler for functions returning promises that can limit their concurrency.
- Host: GitHub
- URL: https://github.com/fabiospampinato/promise-concurrency-limiter
- Owner: fabiospampinato
- License: mit
- Created: 2020-12-29T20:08:20.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2024-04-04T23:04:20.000Z (9 months ago)
- Last Synced: 2024-11-01T01:47:03.586Z (2 months ago)
- Topics: concurrency, limit, limiter, promise
- Language: TypeScript
- Size: 8.79 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# Promise Concurrency Limiter
Tiny scheduler for functions returning promises that can limit their concurrency.
## Install
```sh
npm install --save promise-concurrency-limiter
```## Usage
```ts
import Limiter from 'promise-concurrency-limiter';const limiter = new Limiter ({
concurrency: 2 // Limit the number of simultaneously active promises to 2
});const somePromiseReturningFunction = async () => { /* ... */ };
limiter.add ( somePromiseReturningFunction ); // First function added, executed immediately
limiter.add ( somePromiseReturningFunction ); // Second function added, executed immediately
limiter.add ( somePromiseReturningFunction ); // Third function added, executed immediately only if one of the 2 available slots got freed, deferred otherwise
```## Deadlocks
Note that if your concurrency-limited functions can also schedule other concurrency-limited functions you can get in a deadlock situation, where if the concurrency limit is N, and you have N currently executing functions, but all of those functions need to schedule other functions before they can resolve, then nothing will happen because all the available spots are filled already, and no current function will ever resolve.
For situations like that, unless you have some assurances that a deadlock can't actually happen, this library may not be the appropriate way to limit concurrency for your use case.
## License
MIT © Fabio Spampinato