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>
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 )
Function to repeat
Value to compare with return of functionToCall to exit repetition
Timespan in milliseconds to repeat functionToCall to match expectedValue before giving up
Timespan in milliseconds to wait before next repetition
optional: Callback on start of new interval
(passedTime) repeatFunctionCallTimer( ()=>console.log(new Date().getSeconds()), '', 3000, 1000 );
// ... meanhwile something else ...
await repeatFunctionCallTimer( ()=>console.log(new Date().getSeconds()), '', 3000, 1000 );
// ... afterwards something else ...
function checkIfValueSetByAnotherAsynchronousProcess(){
// ... code ...
if(valueToBeSetByAnotherProcess) return true;
return false;
}
const res = await repeatFunctionCallTimer( checkIfValueSetByAnotherAsynchronousProcess, true, 2000, 100 )
if(res.match){ // ... something only if value was set in given time }
Do different things depending if another asynchronous process set a value
function checkIfValueSetByAnotherAsynchronousProcess(){
// ... code ...
if(valueToBeSetByAnotherProcess) return true;
return false;
}
const res = await repeatFunctionCallTimer( checkIfValueSetByAnotherAsynchronousProcess, true, 2000, 100 )
.then(res=>{
if(res.match){ // ... something only if value was set in given time }
else{ // ... something if value was no set in given time }
})
See demos
Be aware that timers in JavaScript are not exact, depending e.g. on the current Call stack. They are reliable for “normal” checking and ordering, but not for high precision orchestration which is clearly not the scope here.
The functionToCall can not be an asynchronous function / Promise or a function returning an asynchronous function / Promise since this would obviously collide with the synchronous nature of intervals and timeouts. Of course this does not mean that you can not wait for an output of an asynchronous function / Promise.
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