Modern version of setInterval for promises and async functions available in Node.js and browsers.
setIntervalAsync works both on Node.js and in the browser, providing the same
familiar interface as
setInterval for asynchronous functions, while preventing
multiple executions from overlapping in time.
You can install
setIntervalAsync using npm:
npm install -E set-interval-async
Or using Yarn:
yarn add -E set-interval-async
Now, you can require
setIntervalAsync in CommonJS:
// Choose one of the following flavors: dynamic, fixed, legacy.const setIntervalAsync clearIntervalAsync =const setIntervalAsync clearIntervalAsync =const setIntervalAsync clearIntervalAsync =// Or require all at once:constdynamic: setIntervalAsync: setIntervalAsyncDfixed: setIntervalAsync: setIntervalAsyncFlegacy: setIntervalAsync: setIntervalAsyncLclearIntervalAsync} =
Or else, you can use ES6 modules syntax:
// Choose one of the following flavors: dynamic, fixed, legacy.// Import all at once:const setIntervalAsync: setIntervalAsyncD = dynamicconst setIntervalAsync: setIntervalAsyncF = fixedconst setIntervalAsync: setIntervalAsyncL = legacy
In the browser, you can add a script tag in your HTML:
After the script is loaded, a module
SetIntervalAsync will be defined in the global context.
Now, you can retrieve the
setIntervalAsync function in any of its flavors:
// Choose one of the following flavors: dynamic, fixed, legacy.var setIntervalAsync = SetIntervalAsyncdynamicsetIntervalAsyncvar setIntervalAsync = SetIntervalAsyncfixedsetIntervalAsyncvar setIntervalAsync = SetIntervalAsynclegacysetIntervalAsync// Load `clearIntervalAsync` as well.var clearIntervalAsync = SetIntervalAsyncclearIntervalAsync
If you've ever had to deal with weird, subtle bugs as a consequence of using
setInterval on asynchronous functions, or had to manually reimplement
setTimeout to prevent multiple executions of the same asynchronous function from overlapping, then this library is a drop-in replacement that will solve your issues.
setInterval runs a given function repeateadly, once every fixed number of milliseconds. This may cause problems whenever the function takes longer to execute than the given interval, since it will be called again before the first execution finished. This is often a problem for non-reentrant functions; ie. functions that are not designed to allow multiple executions at the same time.
setIntervalAsync is a drop-in replacement of
setInterval which shares the same API but is safe to use with non-reentrant, asynchronous functions.
Dynamic and Fixed
setIntervalAsync provides two strategies which can be used to prevent a recurring function from executing more than once at any given moment:
Dynamic: If possible, the given function is called once every
intervalmilliseconds. If any execution takes longer than the desired interval, the next execution is delayed until the previous one has finished, and called immediately after this condition is reached.
Fixed: The given function is called repeatedly, guaranteeing a fixed delay of
intervalmilliseconds between the end of one execution and the start of the following one.
You can choose whichever strategy works best for your application. When in doubt, the
Dynamic strategy will likely suffice for most use cases, keeping the interval as close as possible to the desired one.
To see a full set of examples and how to run them, check out our examples directory.
You can browse the full API in our Documentation page.
In order to contribute to this project, you will need to first clone the repository:
git clone https://github.com/ealmansi/set-interval-async.git
Make sure that Yarn is installed globally on your system, install all project dependencies, and build the project:
Now, you can run the tests and make sure that everything is up and running correctly:
If the previous step succeeds, you're ready to start developing on this project.
Pull requests are welcome!