Keeps calling callback until it returns something other than undefined.
There is an exponentially-increasing delay between each retry attempt.
If callback throws an exception, the retry is cancelled, bubbling the exception.
// A function that only works some of the time constflakyFn = async () => { // do the thing if (Math.random() > 0.9) returntrue; // success return; // fake failure };
// Retry it up to five times, // starting with 1000ms interval constresult = awaitretryFunction(flakyFn, { limitAttempts:5 });
if (result.success) { // Yay } else { console.log(`Failed after ${result.attempts} attempts. Elapsed: ${result.elapsed}`); console.log(result.message); }
Keeps calling
callback
until it returns something other than undefined. There is an exponentially-increasing delay between each retry attempt.If
callback
throws an exception, the retry is cancelled, bubbling the exception.An
AbortSignal
can be used to cancel process.