# limit-concurrency-decorator

> Decorator to limit concurrency of async functions

Latest version **0.6.0** (published 2024-01-18) · ISC license · 0 weekly downloads

## Install

```sh
npm install limit-concurrency-decorator
pnpm add limit-concurrency-decorator
yarn add limit-concurrency-decorator
bun add limit-concurrency-decorator
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.0 |
| Published | 2024-01-18 |
| First published | 2017-07-28 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=7.6 |
| Dependencies | 0 |
| Unpacked size | 95.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Julien Fontanet |
| Maintainers | julien-f, pdonias |
| Keywords | async, concurrency, decorator, parallelism, promise, limit |

## Links

- npm: https://www.npmjs.com/package/limit-concurrency-decorator
- Repository: https://github.com/JsCommunity/limit-concurrency-decorator
- Issues: https://github.com/JsCommunity/limit-concurrency-decorator/issues
- npm.io page: https://npm.io/package/limit-concurrency-decorator

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 0.6.0 (latest) — 2024-01-18
- 0.5.0 — 2021-05-18
- 0.4.0 — 2018-02-21
- 0.3.0 — 2018-02-14
- 0.2.0 — 2017-09-07
- 0.1.0 — 2017-08-29
- 0.0.0 — 2017-07-28

## README

# limit-concurrency-decorator

[![Package Version](https://badgen.net/npm/v/limit-concurrency-decorator)](https://npmjs.org/package/limit-concurrency-decorator) [![Build Status](https://travis-ci.org/JsCommunity/limit-concurrency-decorator.png?branch=master)](https://travis-ci.org/JsCommunity/limit-concurrency-decorator) [![PackagePhobia](https://badgen.net/packagephobia/install/limit-concurrency-decorator)](https://packagephobia.now.sh/result?p=limit-concurrency-decorator) [![Latest Commit](https://badgen.net/github/last-commit/JsCommunity/limit-concurrency-decorator)](https://github.com/JsCommunity/limit-concurrency-decorator/commits/master)

> Decorator to limit concurrency of async functions

Similar to these libraries, but can be used as decorator:

- [throat](https://github.com/ForbesLindesay/throat)
- [p-limit](https://github.com/sindresorhus/p-limit)

Also similar to
[p-concurrency](https://github.com/kaelzhang/p-concurrency), but the
limit can be enforced over multiple functions.

## Install

Installation of the [npm package](https://npmjs.org/package/limit-concurrency-decorator):

```
> npm install --save limit-concurrency-decorator
```

## Usage

Simply apply the decorator to a method:

```js
import { limitConcurrency } from "limit-concurrency-decorator";

class HttpClient {
  @limitConcurrency(2)
  get() {
    // ...
  }
}

const client = new HttpClient();

// these calls will run in parallel
client.get("http://example.net/");
client.get("http://example2.net/");

// this call will wait for one of the 2 previous to finish
client.get("http://example3.net/");
```

Or a simple function as a wrapper:

```js
import httpRequest from "http-request-plus";

const httpRequestLimited = limitConcurrency(2)(httpRequest);

// these calls will run in parallel
httpRequestLimited("http://example.net/");
httpRequestLimited("http://example2.net/");

// this call will wait for one of the 2 previous to finish
httpRequestLimited("http://example3.net/");
```

Or even as a call limiter:

```js
const limiter = limitConcurrency(2)(/* nothing */);

// these calls will run in parallel
limiter(asyncFn, param1, ...);
limiter.call(thisArg, asyncFn, param1, ...);

// this call will wait for one of the 2 previous to finish
limiter.call(thisArg, methodName, param1, ...)
```

The limit can be shared:

```js
const myLimit = limitConcurrency(2);

class HttpClient {
  @myLimit
  post() {
    // ...
  }

  @myLimit
  put() {
    // ...
  }
}
```

With `FAIL_ON_QUEUE` you can fail early instead of waiting:

```js
import { FAIL_ON_QUEUE } from "limit-concurrency-decorator";

try {
  await httpRequestLimited(FAIL_ON_QUEUE, "http://example2.net");
} catch (error) {
  error.message; // 'no available place in queue'
}
```

Custom termination:

```js
const httpRequestLimited = limitConcurrency(2, async (promise) => {
  const stream = await promise;
  await new Promise((resolve) => {
    stream.on("end", resolve);
    stream.on("error", reject);
  });
})(httpRequest);

// these calls will run in parallel
httpRequestLimited("http://example.net/");
httpRequestLimited("http://example2.net/");

// this call will wait for one of the 2 previous responses to have been read entirely
httpRequestLimited("http://example3.net/");
```

## Contributions

Contributions are _very_ welcomed, either on the documentation or on
the code.

You may:

- report any [issue](https://github.com/JsCommunity/limit-concurrency-decorator/issues)
  you've encountered;
- fork and create a pull request.

## License

ISC © [Julien Fontanet](https://github.com/julien-f)

---
_Source: https://npm.io/package/limit-concurrency-decorator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
