Have ideas to improve npm?Join in the discussion! ¬Ľ

    set-interval-async
    DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/set-interval-async package

    1.0.13¬†‚Äʬ†Public¬†‚Äʬ†Published

    setIntervalAsync
    License: MIT npm version Build Status Coverage Status PRs Welcome

    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.

    NPM JavaScript Style Guide

    Getting Started

    Node.js

    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 } = require('set-interval-async/dynamic')
    const { setIntervalAsync, clearIntervalAsync } = require('set-interval-async/fixed')
    const { setIntervalAsync, clearIntervalAsync } = require('set-interval-async/legacy')
     
    // Or require all at once:
     
    const {
      dynamic: { setIntervalAsync: setIntervalAsyncD },
      fixed: { setIntervalAsync: setIntervalAsyncF },
      legacy: { setIntervalAsync: setIntervalAsyncL },
      clearIntervalAsync 
    } = require('set-interval-async')

    Or else, you can use ES6 modules syntax:

    // Choose one of the following flavors: dynamic, fixed, legacy.
     
    import { setIntervalAsync, clearIntervalAsync } from 'set-interval-async/dynamic'
    import { setIntervalAsync, clearIntervalAsync } from 'set-interval-async/fixed'
    import { setIntervalAsync, clearIntervalAsync } from 'set-interval-async/legacy'
     
    // Import all at once:
     
    import {
      dynamic,
      fixed,
      legacy,
      clearIntervalAsync
    } from 'set-interval-async'
    const { setIntervalAsync: setIntervalAsyncD } = dynamic
    const { setIntervalAsync: setIntervalAsyncF } = fixed
    const { setIntervalAsync: setIntervalAsyncL } = legacy
     

    Browser

    In the browser, you can add a script tag in your HTML:

    <script src="https://unpkg.com/set-interval-async"></script>

    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 = SetIntervalAsync.dynamic.setIntervalAsync
    var setIntervalAsync = SetIntervalAsync.fixed.setIntervalAsync
    var setIntervalAsync = SetIntervalAsync.legacy.setIntervalAsync
     
    // Load `clearIntervalAsync` as well.
     
    var clearIntervalAsync = SetIntervalAsync.clearIntervalAsync

    Motivation

    If you've ever had to deal with weird, subtle bugs as a consequence of using setInterval[1] on asynchronous functions, or had to manually reimplement setInterval using setTimeout[2] 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.

    [1] https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout
    [2] https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval

    Dynamic and Fixed setIntervalAsync

    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 interval milliseconds. 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.

      Dynamic setIntervalAsync diagram.

    • Fixed: The given function is called repeatedly, guaranteeing a fixed delay of interval milliseconds between the end of one execution and the start of the following one.

      Fixed setIntervalAsync diagram.

    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.

    Examples

    To see a full set of examples and how to run them, check out our examples directory.

    Documentation

    You can browse the full API in our Documentation page.

    Contributing

    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:

    yarn
    yarn build

    Now, you can run the tests and make sure that everything is up and running correctly:

    yarn test

    If the previous step succeeds, you're ready to start developing on this project.
    Pull requests are welcome!

    You can verify that your code follows the JavaScript Standard Style with the following command:

    yarn lint

    Install

    npm i [email protected]

    Version

    1.0.13

    License

    MIT

    Unpacked Size

    218 kB

    Total Files

    29

    Last publish

    Collaborators

    • avatar