This specialized function takes an input function as its first argument and a set of customizable options as its second. It repeatedly calls the provided function, retrying until either a successful result is returned or the maximum number of attempts is reached. Any outcome from the function, whether it returns a value or throws an error, is made accessible to external users.
The challenge at hand:
I'm tasked with typing this function in such a way that any parameter-less function can be retried without encountering type errors in the index.ts file. The original return type of the input function must remain intact, enclosed within a promise structure.
function wait(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export async function retryFn(fn, { tries, interval }) {
try {
return fn();
} catch (e) {
const newTries = tries - 1;
if (newTries === 0) {
throw e;
}
await wait(interval);
return retryFn(fn, { tries: newTries, interval });
}
}