Skip to content

Instantly share code, notes, and snippets.

@mauriciord
Last active March 12, 2019 13:42
Show Gist options
  • Save mauriciord/8da86640073283c8f8cb69bc61eb24e9 to your computer and use it in GitHub Desktop.
Save mauriciord/8da86640073283c8f8cb69bc61eb24e9 to your computer and use it in GitHub Desktop.
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