Skip to content

Instantly share code, notes, and snippets.

@snahor
Last active Sep 28, 2016
Embed
What would you like to do?
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