Loading problem…
Implement a utility promiseRetry that retries a promise-returning function when it fails.
fn that returns a promisemaxRetries timesfunction promiseRetry(fn, options = {})Where options supports:
maxRetries (default: 3)initialDelay in ms (default: 100)backoffMultiplier (default: 2)// Example: Retries until success
let attempts = 0;
const fn = () => {
attempts++;
return attempts < 3 ? Promise.reject('fail') : Promise.resolve('ok');
};
promiseRetry(fn, { maxRetries: 3, initialDelay: 10 }).then(value => {
console.log(value); // 'ok' (after 3 attempts)
});
// Example: Rejects after max retries
const failingFn = () => Promise.reject('error');
promiseRetry(failingFn, { maxRetries: 2, initialDelay: 50 }).catch(error => {
console.log(error); // 'error' (last failure after 2 attempts)
});