Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/perry-mitchell/delayable-setinterval
An asynchronous setInterval that can be delayed using promises
https://github.com/perry-mitchell/delayable-setinterval
interval promise setinterval settimeout timer timers
Last synced: 23 days ago
JSON representation
An asynchronous setInterval that can be delayed using promises
- Host: GitHub
- URL: https://github.com/perry-mitchell/delayable-setinterval
- Owner: perry-mitchell
- License: mit
- Created: 2019-04-12T07:11:33.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-08-27T18:23:27.000Z (about 1 year ago)
- Last Synced: 2024-10-08T23:41:19.415Z (about 1 month ago)
- Topics: interval, promise, setinterval, settimeout, timer, timers
- Language: JavaScript
- Size: 76.2 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Delayable setInterval
> An asynchronous setInterval that can be delayed using promises![build status](https://github.com/perry-mitchell/delayable-setinterval/actions/workflows/test.yml/badge.svg)
## About
JavaScript's `setInterval` works in a synchronous manner which is non-ideal for handling Promises or asynchronous methods. `setDelayedInterval` matches `setInterval`'s functionality besides the handling of _thenable_ return values.
If a callback of `setDelayedInterval` returns a `Promise`, the interval timer is delayed by the execution of that promise.
![Delayable setInterval timing example](example.jpg)
The timer, if not presented with asynchronous results from callbacks, will execute at expected intervals. Upon receiving a promise the starting of the next interval timer will be delayed as shown above.
## Usage
Usage is much the same as `setInterval`:
```javascript
const { clearDelayedInterval, setDelayedInterval } = require("delayable-setinterval");const interval = setDelayedInterval(async () => {
await someAsyncTask();
}, 1000);// Later
clearDelayedInterval(interval);
```The example above uses an `async` method to perform some asynchronous tasks. The method is fired every `1000`ms, but is delayed between initialisations of the timer by the time it takes to complete the async callback.
## Support
This library supports NodeJS 12+.