Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/seregpie/throttleasync
Creates a throttled function.
https://github.com/seregpie/throttleasync
async delay function promise throttle
Last synced: about 1 month ago
JSON representation
Creates a throttled function.
- Host: GitHub
- URL: https://github.com/seregpie/throttleasync
- Owner: SeregPie
- License: mit
- Created: 2020-04-06T12:31:12.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-04-07T15:41:26.000Z (almost 5 years ago)
- Last Synced: 2024-11-20T22:59:09.615Z (2 months ago)
- Topics: async, delay, function, promise, throttle
- Language: JavaScript
- Size: 3.91 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# throttleAsync
`throttleAsync(func, delay = 0, trailing = false)`
Creates a throttled function.
| argument | description |
| ---: | :--- |
| `func` | A function to throttle. |
| `delay` | A number as the delay. |
| `trailing` | If `true`, the function is invoked on the trailing edge of the delay. |Returns a new function.
## setup
### npm
```shell
npm install @seregpie/throttle-async
```### ES module
```javascript
import throttleAsync from '@seregpie/throttle-async';
```### Node
```javascript
let throttleAsync = require('@seregpie/throttle-async');
```### browser
```html
```
The function is globally available as `throttleAsync`.
## usage
```javascript
let throttled = throttleAsync(async n => {
await new Promise(resolve => {
setTimeout(resolve, 2000);
});
return n;
}, 1000);
let p0 = throttled(0);
let p1 = throttled(1);
let p2 = throttled(2);
await new Promise(resolve => {
setTimeout(resolve, 3000);
});
let p3 = throttled(3);
let p4 = throttled(4);
let p5 = throttled(5);
let r = await Promise.all([p0, p1, p2, p3, p4, p5]);
// => [0, 2, 2, 3, 5, 5]
```