Skip to content

Instantly share code, notes, and snippets.

@AlekseyA
Created December 27, 2019 10:38
Show Gist options
  • Select an option

  • Save AlekseyA/b9831dad3ebf85bceb9064c352f9b6a7 to your computer and use it in GitHub Desktop.

Select an option

Save AlekseyA/b9831dad3ebf85bceb9064c352f9b6a7 to your computer and use it in GitHub Desktop.
Async backoff
(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