Created
April 11, 2021 08:09
-
-
Save hubgit/65d674e9cd9a8a3c4c39b458efef5a0b to your computer and use it in GitHub Desktop.
Fetch with retries
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
| const robustFetch = async (url, tries = 3) => { | |
| let response | |
| do { | |
| try { | |
| response = await fetch(url) | |
| // TODO: check for response.ok | |
| return response | |
| } catch (error) { | |
| console.error(error) | |
| tries-- | |
| } | |
| // TODO: back off | |
| await new Promise((resolve) => window.setTimeout(resolve, 1000)) | |
| } while (tries) | |
| throw new Error(`Failed to fetch ${url}`) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment