npm package

Repeat function-call timer

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

Created for handling / observing intermediate or final states of asynchronous processes (see Details), but also helpful for other things (see Examples).

hh lohmann <hh.lohmann@gmail.com>

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

As observer

For plain repetition

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

Tests

Source Code

References

MDN: await
MDN: Call Stack
MDN: Primitive Types (JavaScript)
MDN: Recursion
MDN: resolve
MDN: setTimeout
MDN: thenables
MDN: Using promises
MDN: Working with asynchronous functions