Clarify code
This commit is contained in:
@@ -3,18 +3,17 @@ import {isNetworkError} from '#/lib/strings/errors'
|
|||||||
|
|
||||||
export async function retry<P>(
|
export async function retry<P>(
|
||||||
retries: number,
|
retries: number,
|
||||||
cond: (err: any) => boolean,
|
shouldRetry: (err: any) => boolean,
|
||||||
fn: () => Promise<P>,
|
action: () => Promise<P>,
|
||||||
delay?: number,
|
delay?: number,
|
||||||
): Promise<P> {
|
): Promise<P> {
|
||||||
let lastErr
|
let lastErr
|
||||||
while (retries > 0) {
|
while (retries > 0) {
|
||||||
try {
|
try {
|
||||||
return await fn()
|
return await action()
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
lastErr = e
|
lastErr = e
|
||||||
// should retry?
|
if (shouldRetry(e)) {
|
||||||
if (cond(e)) {
|
|
||||||
if (delay) {
|
if (delay) {
|
||||||
await timeout(delay)
|
await timeout(delay)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user