Skip to content

Instantly share code, notes, and snippets.

@snahor
Last active September 28, 2016 07:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snahor/d5fd0f4af461ab97dba2 to your computer and use it in GitHub Desktop.
Save snahor/d5fd0f4af461ab97dba2 to your computer and use it in GitHub Desktop.
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