Created
December 27, 2019 10:38
-
-
Save AlekseyA/b9831dad3ebf85bceb9064c352f9b6a7 to your computer and use it in GitHub Desktop.
Async backoff
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
| (async () => { | |
| let a = 0; | |
| const func = (...args) => { | |
| a++; | |
| return new Promise((res, rej) => { | |
| setTimeout(() => res('ok'), 1000) | |
| // setTimeout(() => rej('fuck'), 1000) | |
| }) | |
| } | |
| const pause = (duration) => new Promise(res => setTimeout(res, duration)); | |
| const backoff = (fn, retries, delay = 100) => { | |
| return fn().catch(err => { | |
| console.log('delay', delay) | |
| return retries > 1 | |
| ? pause(delay).then(() => backoff(fn, retries - 1, delay * 4)) | |
| : Promise.reject(err) | |
| }); | |
| } | |
| const run = async () => await func(12) | |
| const createRequest = () => { | |
| } | |
| result = await backoff(func, 4).catch(err => console.log('FUCK:', err)); | |
| console.log('Resolved', result) | |
| })() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment