1.4.0 • Published 4 years ago

interval-promise v1.4.0

Weekly downloads
7,094
License
MIT
Repository
github
Last release
4 years ago

Interval Promise

NPM Version Build Status Coverage Status

Overview

This library provides a simple mechanism for running a promise with a given amount of time between executions.

Standard Javascript » setInterval()

traditional interval

interval-promise » interval()

interval promise

Installation

npm install interval-promise

Usage

Simple example using async-await

const interval = require('interval-promise')

// Run a function 10 times with 1 second between each iteration
interval(async () => {
    await someOtherPromiseReturningFunction()
    await another()
}, 1000, {iterations: 10})

API

interval(func, intervalLength, options = {}) // returns Promise<undefined>

Arguments

Project Values

  • Approachability — Basic usage should be concise and readable.
  • Debuggability — Error feedback should be helpful and error handling options should be flexible.
  • Stability — Functionality should be well-tested and reliable.

Acknowledgements

This library was inspired by reissue.

FAQ

How can I stop the interval from outside the interval function?

There isn't currently direct feature to stop the iterations externally. You can, however, achieve this by checking a variable in the parent scope (of where the function is defined). Check out the code below.

const interval = require('interval-promise')

let stoppedExternally = false
const stopExternally = () => { stoppedExternally = true }

interval(async (iteration, stop) => {

    if (stoppedExternally) {
        stop()
    }
    
    // ... normal functionality ...
    
}, 1000)

// Some other work...
someOtherWork().then(() => {

    // Now that our "other work" is done, we can stop our interval above with:
    stopExternally()

})