Last active
April 16, 2026 20:12
-
-
Save antlis/1cbf7224e1b1500740a7ed3ca86426e2 to your computer and use it in GitHub Desktop.
Retries async operations with exponential backoff. Useful for flaky network requests or transient failures.
This file contains hidden or 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
| export async function retry(fn, { | |
| retries = 3, | |
| delay = 300, | |
| factor = 2 | |
| } = {}) { | |
| let attempt = 0; | |
| while (true) { | |
| try { | |
| return await fn(); | |
| } catch (err) { | |
| if (attempt >= retries) throw err; | |
| const wait = delay * Math.pow(factor, attempt); | |
| await new Promise(r => setTimeout(r, wait)); | |
| attempt++; | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use case: