-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
58 lines (55 loc) · 1.49 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* Retry a function until it does not throw an exception.
*
* @param fn the function to execute
* @param {RetryOptions} retryOptions retry options
*/
export async function retry<T>(
fn: () => T,
retryOptions: RetryOptions,
): Promise<T> {
const wrapped = () =>
new Promise<T>((resolve, reject) => {
try {
const result = fn();
resolve(result);
} catch (err) {
reject(err);
}
});
return retryAsync(wrapped, retryOptions);
}
/**
* Retry an async function until it does not throw an exception.
*
* @param fn the function to execute
* @param {RetryOptions} retryOptions retry options
*/
export async function retryAsync<T>(
fn: () => Promise<T>,
{ maxTry, delay: delay }: RetryOptions,
): Promise<T> {
try {
return await fn();
} catch (err) {
if (maxTry > 1) {
await wait(delay);
return await retryAsync(fn, { delay: delay, maxTry: maxTry - 1 });
}
throw err;
}
}
/**
* Retry options:
* @typedef {Object} RetryOptions
* @property {number} maxTry: maximum number of attempts. if fn is still throwing execption afect maxtry attempts, an exepction is thrown
* @property {number} delay: number of miliseconds between each attempt.
*/
export interface RetryOptions {
maxTry: number;
delay: number;
}
/** An async function that does nothing during a number of milliseconds */
export function wait(duration: number): Promise<void> {
return new Promise<void>((resolve) => setTimeout(resolve, duration));
}