Last active
September 28, 2016 07:34
-
-
Save snahor/d5fd0f4af461ab97dba2 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
function* fib() { | |
let a = 0; | |
let b = 1; | |
while (true) { | |
[a, b] = [b, a + b]; | |
yield a; | |
} | |
} | |
const sleep = async (n) => await (new Promise(r => setTimeout(r, n))); | |
const retry = async (fn, retries = 10) => { | |
const gen = fib(); | |
const inner = async (retries) => { | |
let result = await fn(); | |
if (!result && retries) { | |
const ms = 1000 * gen.next().value; | |
console.log(`Retrying in ${ms} milliseconds`); | |
await sleep(ms); | |
return await inner(retries - 1); | |
} | |
return result; | |
} | |
return await inner(retries); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment