We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 72ed673 commit aa64258Copy full SHA for aa64258
src/utils/retryUtils.ts
@@ -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