# p-throttler

> A promise based throttler capable of limiting execution of parallel tasks

Latest version **1.0.2** (published 2016-01-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install p-throttler
pnpm add p-throttler
yarn add p-throttler
bun add p-throttler
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2016-01-03 |
| First published | 2013-05-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.12 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 12 |
| Author | IndigoUnited |
| Maintainers | satazor |
| Keywords | promise, throttler, trottle, deferred, worker, tasks, parallel |

## Links

- npm: https://www.npmjs.com/package/p-throttler
- Repository: https://github.com/IndigoUnited/node-p-throttler
- Homepage: https://github.com/IndigoUnited/node-p-throttler#readme
- Issues: http://github.com/IndigoUnited/node-p-throttler/issues
- npm.io page: https://npm.io/package/p-throttler

## Alternatives

- [cron](https://npm.io/package/cron.md) — 4.9M weekly downloads
- [@vercel/queue](https://npm.io/package/@vercel/queue.md) — 731.6K weekly downloads
- [create-sonicjs](https://npm.io/package/create-sonicjs.md) — 1.6K weekly downloads
- [@exellix/jobs-api](https://npm.io/package/@exellix/jobs-api.md) — 941 weekly downloads
- [@forwardimpact/libskill](https://npm.io/package/@forwardimpact/libskill.md) — 575 weekly downloads

## Recent versions

- 1.0.2 (latest) — 2016-01-03
- 1.0.1 — 2016-01-02
- 1.0.0 — 2016-01-02
- 0.1.1 — 2014-09-08
- 0.1.0 — 2014-09-07
- 0.0.1 — 2013-05-27

## README

# p-throttler

[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency status][david-dm-image]][david-dm-url] [![Dev Dependency status][david-dm-dev-image]][david-dm-dev-url]

[npm-url]:https://npmjs.org/package/p-throttler
[downloads-image]:http://img.shields.io/npm/dm/p-throttler.svg
[npm-image]:http://img.shields.io/npm/v/p-throttler.svg
[travis-url]:https://travis-ci.org/IndigoUnited/node-p-throttler
[travis-image]:http://img.shields.io/travis/IndigoUnited/node-p-throttler.svg
[david-dm-url]:https://david-dm.org/IndigoUnited/node-p-throttler
[david-dm-image]:https://img.shields.io/david/IndigoUnited/node-p-throttler.svg
[david-dm-dev-url]:https://david-dm.org/IndigoUnited/node-p-throttler#info=devDependencies
[david-dm-dev-image]:https://img.shields.io/david/dev/IndigoUnited/node-p-throttler.svg

A promise based throttler responsible for limiting execution of parallel tasks.
The number of parallel tasks may be limited and configured per type.


## Installation

`$ npm install p-throttler`


## API

### #create(defaultConcurrency, types)

Constructs a new throttler.

The `defaultConcurrency` is the default maximum concurrent functions being run (-1 to specify no limits).   
The `types` allows you to specify different concurrencies for different types.   

Example:

```js
var throttler = PThrottler.create(15, {  // or new PThrottler()
    'network_io': 10,
    'disk_io': 50
});
```


### .enqueue(func, [type]): Promise

Enqueues a function to be ran. The function is expected to return a promise or a value.   
The returned promise is resolved when the function finishes execution.

The `type` argument is optional and can be a `string` or an array of `strings`.   
Use it to specify the type(s) associated with the function.   

The function will run as soon as a free slot is available for every `type`.  
If no `type` is passed or is unknown, the `defaultConcurrency` is used.  

The execution order is guaranteed for functions enqueued with the exact same `type` argument.

Example:

```js

var method = function () {
    return Q.resolve('foo');
};

var throttler = PThrottler.create(15, {
    'foo': 1,
    'bar': 2
});

// Single type, will only run when a free slot for
// "foo" is available
throttler.enqueue(function () {
    return method();    // method() returns some promise
}, 'foo')
.then(function (value) {
    console.log(value);
});

// Multiple type, will only run when a free slot for
// "foo" and "bar" are available
throttler.enqueue(function () {
    return method();    // method() returns some promise
}, ['foo', 'bar'])
.then(function (value) {
    console.log(value);
});
```


### .abort(): Promise

Aborts all current work being done.
Returns a promise that is resolved when the current running functions finish to execute.   
Any function that was in the queue waiting to be ran is removed immediately.


## License

Released under the [MIT License](http://www.opensource.org/licenses/mit-license.php).

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