Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tomeraberbach/limit-concur
⚖️ Limit an async function's concurrency with ease!
https://github.com/tomeraberbach/limit-concur
async concurrency javascript nodejs npm-module npm-package promise rate-limiting
Last synced: 18 days ago
JSON representation
⚖️ Limit an async function's concurrency with ease!
- Host: GitHub
- URL: https://github.com/tomeraberbach/limit-concur
- Owner: TomerAberbach
- License: apache-2.0
- Created: 2021-04-04T02:56:44.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-06-23T02:58:07.000Z (5 months ago)
- Last Synced: 2024-10-12T01:23:29.870Z (about 1 month ago)
- Topics: async, concurrency, javascript, nodejs, npm-module, npm-package, promise, rate-limiting
- Language: TypeScript
- Homepage: https://npm.im/limit-concur
- Size: 437 KB
- Stars: 21
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- Contributing: contributing.md
- License: license
Awesome Lists containing this project
README
limit-concur
Limit an async function's concurrency with ease!## Features
- **Tiny:** only ~320 B minzipped
- **Flexible:** works for any function that returns a `Promise`## Install
```sh
$ npm i limit-concur
```## Usage
```js
import got from 'got'
import limitConcur from 'limit-concur'const categories = await got(
`https://api.chucknorris.io/jokes/categories`,
).json()const getChuckNorrisJoke = async category => {
const { value } = await got(`https://api.chucknorris.io/jokes/random`, {
searchParams: {
category,
},
}).json()
return value
}const limitedGetChuckNorrisJoke = limitConcur(4, getChuckNorrisJoke)
// At most 4 requests are pending at any given time!
const jokes = await Promise.all(categories.map(limitedGetChuckNorrisJoke))
console.log(jokes)
//=> [
// 'Chuck Norris once rode a nine foot grizzly bear through an automatic car wash, instead of taking a shower.',
// "Chuck Norris is actually the front man for Apple. He let's Steve Jobs run the show when he's on a mission. Chuck Norris is always on a mission.",
// "Bill Gates thinks he's Chuck Norris. Chuck Norris actually laughed. Once.",
// 'Chuck Norris can install iTunes without installing Quicktime.',
// ...
// ]
```You can get an API equivalent to
[`p-limit`](https://github.com/sindresorhus/p-limit) like so:```js
import limitConcur from 'limit-concur'const limit = limitConcur(1, fn => fn())
const input = [
limit(() => fetchSomething(`foo`)),
limit(() => fetchSomething(`bar`)),
limit(() => doSomething()),
]const result = await Promise.all(input)
console.log(result)
//=> [ ..., ..., ... ]
```## Contributing
Stars are always welcome!
For bugs and feature requests,
[please create an issue](https://github.com/TomerAberbach/limit-concur/issues/new).For pull requests, please read the
[contributing guidelines](https://github.com/TomerAberbach/limit-concur/blob/main/contributing.md).## License
[Apache License 2.0](https://github.com/TomerAberbach/limit-concur/blob/main/license)
This is not an official Google product.