repeat-function-call-timer

npm package

Repeat function-call timer

Repeat or retry a function call until it returns a specific value or a timeout is reached.

Possibly also helpful for awaiting the resolution of an asynchronous function / Promise when a “real” await is not possible (see also Caveats).

hh lohmann <hh.lohmann@gmail.com>

This page may be displayed more optimal in its GitHub Pages view

Synopsis

  import { repeatFunctionCallTimer } from 'repeat-function-call-timer'

  repeatFunctionCallTimer( functionToCall, expectedValue, timeout, interval )

  await repeatFunctionCallTimer( functionToCall, expectedValue, timeout, interval )

  const res = await repeatFunctionCallTimer( functionToCall, expectedValue, timeout, interval )
  if(res.match) ...

  repeatFunctionCallTimer( functionToCall, expectedValue, timeout, interval )
  .then(res=>{
    if(res.match){ ... }
    else{ ... }
  })

  repeatFunctionCallTimer( functionToCall, expectedValue, timeout, interval, whenInterval )

Parameters

functionToCall

Function to repeat

expectedValue

Value to compare with return of functionToCall to exit repetition

timeout

Timespan in milliseconds to repeat functionToCall to match expectedValue before giving up

interval

Timespan in milliseconds to wait before next repetition

whenInterval

optional: Callback on start of new interval

Returns

Examples

Demos

See demos on GitHub Pages for this repo

Caveats

Installation

Pick for your preferred package manager:

  npm i repeat-function-call-timer
  pnpm i repeat-function-call-timer
  bun i repeat-function-call-timer
  # For Yarn you should double check docs for your and / or
  # current Yarn version, newer versions do not treat `i package_name`
  # as an alias for `add ...` and exclude global installations
  yarn add repeat-function-call-timer

Details

References

MDN: await
MDN: Call Stack
MDN: Recursion
MDN: setTimeout
MDN: thenables
MDN: Using promises
MDN: Working with asynchronous functions