export type RetryConfig = { action: () => Promise count?: number callback?: (error: unknown, current: number) => void } export async function retry(config: RetryConfig): Promise export async function retry(action: () => Promise, count?: number): Promise export async function retry(actionOrConfig: RetryConfig | (() => Promise), countNumber?: number) { let current = 1 let { action, count = 2, callback, } = typeof actionOrConfig === "function" ? ({ action: actionOrConfig, count: countNumber } as RetryConfig) : actionOrConfig async function _retry() { try { return await action() } catch (error) { callback?.(error, current) if (count === 1) throw error current++ count-- return await _retry() } } return await _retry() }