Created
February 20, 2022 22:54
-
-
Save itsMapleLeaf/fcd87811f83e61314ee87387e2ea0f0b to your computer and use it in GitHub Desktop.
retry function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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