import { s } from "https://esm.town/v/jrmann100/s";
export async function retry<A extends unknown[], R>(
times = 3,
timeout = 1000,
func: (...args: A) => Promise<R>,
...args: A
): Promise<R> {
try {
return await func(...args);
}
catch (reason) {
if (times > 1)
console.debug(
`${reason}. Will retry${times - 1 !== 1 ? " up to" : ""} ${
times - 1
} more time${s(times)}...`,
);
if (times === 0)
return Promise.reject(reason);
return retry(times - 1, timeout, func, ...args);
}
}