import {timeout} from '#/lib/async/timeout' import {isNetworkError} from '#/lib/strings/errors' export async function retry

( retries: number, shouldRetry: (err: any) => boolean, action: () => Promise

, delay?: number, ): Promise

{ let lastErr while (retries > 0) { try { return await action() } catch (e: any) { lastErr = e if (shouldRetry(e)) { if (delay) { await timeout(delay) } retries-- continue } throw e } } throw lastErr } export async function networkRetry

( retries: number, fn: () => Promise

, ): Promise

{ return retry(retries, isNetworkError, fn) }