Skip to content

Instantly share code, notes, and snippets.

@itsMapleLeaf
Created February 20, 2022 22:54
Show Gist options
  • Save itsMapleLeaf/fcd87811f83e61314ee87387e2ea0f0b to your computer and use it in GitHub Desktop.
Save itsMapleLeaf/fcd87811f83e61314ee87387e2ea0f0b to your computer and use it in GitHub Desktop.
retry function
export async function retry<T>(
times: number,
fn: () => Promise<T>,
): Promise<T> {
let lastError: unknown
while (times > 0) {
try {
return await fn()
} catch (error) {
lastError = error
}
times -= 1
}
throw lastError
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment