Implement a function retryPromise
that retries executing a promise-returning function up to n
times if it fails.
fn
that returns a promise, and an integer n
(maximum retry attempts).fn
resolves at any attempt, return its resolved value immediately.n = 0
(i.e., try only once).let count = 0;
const unreliable = () => {
count++;
return count < 3
? Promise.reject('fail')
: Promise.resolve('success');
};
retryPromise(unreliable, 5).then(console.log).catch(console.error);
// Output: "success"
retryPromise(() => Promise.reject('always fails'), 2)
.then(console.log)
.catch(console.error);
// Output: "always fails"
fn
is always a function that returns a promise.n
is a non-negative integer.