throttle-wait
Run a function at max once per x milliseconds. Subsequent calls will wait.
- 0 dependencies
- Typescript friendly (can use @Decorators)
Installation
$ npm install throttle-wait
or
$ yarn add throttle-wait
Simple usage
// 2020-01-01T00:00:00.114Zawait myFnThrottled // 2020-01-01T00:00:05.120Zawait myFnThrottled // 2020-01-01T00:00:10.125Zawait myFnThrottled
Typescript decorator usage
// 2020-01-01T00:00:00.114Zawait myClass.myFn // 2020-01-01T00:00:05.120Zawait myClass.myFn // 2020-01-01T00:00:10.125Zawait myClass.myFn
IMPORTANT: Make your function async. The decorator will return an async function. This will ensure that the intellisense of your IDE tells you that your function is async.
Options
- max (default=100): The maximum number of calls in the queue before throwing an error
- onThrottle: Callback when your function is being throttled
Errors
Throttle backpressure error: Throttle is being called faster than it can run
This means that your function is being called a lot asynchronously. An error is thrown when there are 100 calls in the queue (configurable).
Advance throttling
If you want to ignore subsequent calls, look at lodash throttle function
If you want to have more throttling control, you can check out these great libs.