a22685c345
* Mark import sort/order/style rules as error * npm run lint -- --fix
30 lines
539 B
TypeScript
30 lines
539 B
TypeScript
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)
|
|
}
|