blob: abf78de55f9be2261b7a995da31613c4ab5c894b (
plain) (
blame)
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
|
import {isNetworkError} from '#/lib/strings/errors'
export async function retry<P>(
retries: number,
cond: (err: any) => boolean,
fn: () => Promise<P>,
): Promise<P> {
let lastErr
while (retries > 0) {
try {
return await fn()
} catch (e: any) {
lastErr = e
if (cond(e)) {
retries--
continue
}
throw e
}
}
throw lastErr
}
export async function networkRetry<P>(
retries: number,
fn: () => Promise<P>,
): Promise<P> {
return retry(retries, isNetworkError, fn)
}
|