Files
dticket.tootaio.com/server/utils/retry.ts
xiaomai 377a9617be feat: implement auth system, passkeys, and user management
Add PostgreSQL and Redis integration for users and sessions
Implement password and WebAuthn passkey login flows
Add Docker stack, super-admin seeding, and protected routes
2026-04-12 20:16:43 +08:00

40 lines
876 B
TypeScript

export interface RetryOptions {
retries?: number
delayMs?: number
factor?: number
label?: string
}
function sleep(delayMs: number) {
return new Promise((resolve) => setTimeout(resolve, delayMs))
}
export async function withRetry<T>(
operation: () => Promise<T>,
options: RetryOptions = {}
) {
const retries = options.retries ?? 10
const delayMs = options.delayMs ?? 1_000
const factor = options.factor ?? 1.5
const label = options.label ?? 'operation'
let attempt = 0
let currentDelay = delayMs
while (true) {
try {
return await operation()
} catch (error) {
attempt += 1
if (attempt > retries) {
throw error
}
console.warn(`${label} failed on attempt ${attempt}. Retrying in ${currentDelay}ms.`)
await sleep(currentDelay)
currentDelay = Math.round(currentDelay * factor)
}
}
}