UNPKG

@cadence-mq/core

Version:

Modern, type-safe, and performant task queue for Node.js

23 lines (19 loc) 456 B
export async function retry<T>( fn: () => Promise<T>, { maxRetries = 0 }: { maxRetries?: number } = {}, ): Promise<T | undefined> { if (maxRetries < 0) { throw new TypeError('maxRetries must be greater than 0'); } let attempts = 0; let lastError: unknown | undefined; while (attempts < maxRetries + 1) { try { return await fn(); } catch (error) { attempts++; lastError = error; } } throw lastError; }