Skip to content

Commit e651fcd

Browse files
authored
Merge pull request #18 from maitamdev/feat/retry-utils
feat(utils): add retry utility
2 parents 72ed673 + aa64258 commit e651fcd

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/utils/retryUtils.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Retry utility for API calls
2+
export interface RetryOptions { maxRetries?: number; delay?: number; backoff?: number; }
3+
export async function withRetry<T>(fn: () => Promise<T>, options: RetryOptions = {}): Promise<T> {
4+
const { maxRetries = 3, delay = 1000, backoff = 2 } = options;
5+
let lastError: Error | undefined;
6+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
7+
try { return await fn(); }
8+
catch (error) {
9+
lastError = error instanceof Error ? error : new Error(String(error));
10+
if (attempt < maxRetries) {
11+
const waitTime = delay * Math.pow(backoff, attempt);
12+
await new Promise(resolve => setTimeout(resolve, waitTime));
13+
}
14+
}
15+
}
16+
throw lastError;
17+
}

0 commit comments

Comments
 (0)