Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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!

Awesome Lists containing this project

README

        


limit-concur



version


CI


gzip size


brotli size


Sponsor


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.