Skip to content

Instantly share code, notes, and snippets.

@basarat
Created October 8, 2018 23:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save basarat/516a3349edffdbc1c1ab095933299bbe to your computer and use it in GitHub Desktop.
Save basarat/516a3349edffdbc1c1ab095933299bbe to your computer and use it in GitHub Desktop.
/**
* Adds retries to an async function.
*/
async function retry<T>(fn: () => Promise<T>, n: number): Promise<T> {
let lastError: any;
for (let index = 0; index < n; index++) {
try {
return await fn();
}
catch (e) {
lastError = e;
}
}
throw lastError;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment