-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b1b48e
commit 4c90e38
Showing
2 changed files
with
37 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export const retryWithBackoff = async <T>( | ||
fn: () => Promise<T>, | ||
retries = 5, | ||
delay = 1000, | ||
): Promise<T> => { | ||
try { | ||
return await fn() | ||
} catch (error: any) { | ||
if (retries > 0 && error.status === 429) { | ||
// Check for 429 status code | ||
// console.warn(`Rate limit hit, retrying in ${delay}ms...`) | ||
await new Promise((resolve) => setTimeout(resolve, delay)) | ||
return retryWithBackoff(fn, retries - 1, delay * 2) // Exponential backoff | ||
} | ||
throw error | ||
} | ||
} |