Skip to content

Instantly share code, notes, and snippets.

@basarat
Created October 8, 2018 23:06
Embed
What would you like to do?
/**
* 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