-
-
Save IlyaIvanchikov/17353c1ba8808218768e1499afaa9b4a to your computer and use it in GitHub Desktop.
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 sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); | |
| async function simpleRetry(fn, retries = 3, delay = 1000) { | |
| try { | |
| return await fn(); | |
| } catch (error) { | |
| if (retries <= 0) throw error; | |
| await sleep(delay) | |
| return simpleRetry(fn, retries - 1, delay); | |
| } | |
| } | |
| // Пример использования | |
| const fetchSomeData = async () => { | |
| return simpleRetry(async () => { | |
| const response = await fetch('https://api.example.com/data'); | |
| return response.json(); | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment