Last active
March 12, 2019 13:42
-
-
Save mauriciord/8da86640073283c8f8cb69bc61eb24e9 to your computer and use it in GitHub Desktop.
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
export default ((functionToCall, url, options, timeout, attempt, maxRetries) => functionToCall(functionToCall, url, options, timeout, attempt, maxRetries))( | |
(functionToCall, url, options, timeout = 7000, attempt = 1, maxRetries = 3) => { | |
return Promise.race([ | |
fetch(url, options), | |
new Promise((_, reject) => | |
setTimeout(() => { | |
if (maxRetries > attempt) { | |
attempt++; | |
return functionToCall(functionToCall, url, options, (timeout * attempt), attempt, maxRetries) | |
} | |
return reject(new Error(`timeout ${attempt}`)); | |
}, timeout) | |
) | |
]); | |
}, | |
url, options, timeout, attempt, maxRetries | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment