1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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);
// await thenWait(timeout);
return retry(times - 1, timeout, func, ...args);
}
}