# generic-throttle

> A lightweight, flexible promise based throttle class perfect for any rate or concurrency limiting need

Latest version **3.1.0** (published 2022-03-28) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install generic-throttle
pnpm add generic-throttle
yarn add generic-throttle
bun add generic-throttle
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.1.0 |
| Published | 2022-03-28 |
| First published | 2017-02-03 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 13.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Tristian Flanagan |
| Maintainers | tflanagan |
| Keywords | throttle, pool, rate, concurrency, limit, quota |

## Links

- npm: https://www.npmjs.com/package/generic-throttle
- Repository: https://github.com/tflanagan/generic-throttle
- Issues: https://github.com/tflanagan/generic-throttle/issues
- npm.io page: https://npm.io/package/generic-throttle

## Dependencies (1)

- [@types/node](https://npm.io/package/@types/node.md) ^13.13.15

## 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

- 3.1.0 (latest) — 2022-03-28
- 3.0.2 — 2022-03-28
- 3.0.1 — 2020-07-24
- 3.0.0 — 2020-07-24
- 2.0.1 — 2020-03-25
- 2.0.0 — 2020-03-25
- 1.3.0 — 2017-10-16
- 1.2.1 — 2017-06-18
- 1.2.0 — 2017-06-18
- 1.1.0 — 2017-05-03
- 1.0.1 — 2017-02-03

## README

generic-throttle
==============

[![npm license](https://img.shields.io/npm/l/generic-throttle.svg)](https://www.npmjs.com/package/generic-throttle) [![npm version](https://img.shields.io/npm/v/generic-throttle.svg)](https://www.npmjs.com/package/generic-throttle) [![npm downloads](https://img.shields.io/npm/dm/generic-throttle.svg)](https://www.npmjs.com/package/generic-throttle)

A lightweight, flexible promise based throttle class perfect for any rate or concurrency limiting need.

Written in TypeScript, targets Nodejs and the Browser

Install
-------
```
# Latest Stable Release
$ npm install generic-throttle

# Latest Commit
$ npm install tflanagan/generic-throttle

# Also available via Bower
$ bower install generic-throttle
```

Documentation
-------------

[TypeDoc Documentation](https://tflanagan.github.io/generic-throttle/)

Rate Limit Example
------------------
This Throttle instance will rate limit the throttle to a maximum of 10 requests
every 30 seconds. It will not throw an error if the maximum is exceeded.

```javascript
'use strict';

const { Throttle } = require('generic-throttle');

const throttle = new Throttle(10, 30000, false);

function delay(){
	return new Promise((resolve) => {
		setTimeout(() => {
			resolve();
		}, 2000);
	});
}

function asyncRequest(i){
	return throttle.acquire(() => {
		console.log('%d does some async request', i);

		return delay();
	}).then(function(){
		console.log('%d finished', i);
	});
}

for(let i = 0; i < 30; ++i){
	asyncRequest(i);
}
```

Concurrency Limit Example
-------------------------
This Throttle instance will limit concurrency to 5 requests at any given
moment. It will not throw an error if the maximum is exceeded.

```javascript
'use strict';

const { Throttle } = require('generic-throttle');

const throttle = new Throttle(5);

function delay(){
	return new Promise((resolve) => {
		setTimeout(() => {
			resolve();
		}, 2000);
	});
}

function asyncRequest(i){
	return throttle.acquire(() => {
		console.log('%d does some async request', i);

		return delay();
	}).then(function(){
		console.log('%d finished', i);
	});
}

for(let i = 0; i < 30; ++i){
	asyncRequest(i);
}
```

License
-------
Copyright 2014 Tristian Flanagan

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

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