Last active
February 25, 2022 14:47
-
-
Save kabukki/f6a18b0cc7cc8116629f5b9f0c8f7462 to your computer and use it in GitHub Desktop.
TS/JS helpers
This file contains 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 const wait = (delay) => new Promise((resolve) => setTimeout(resolve, delay)); | |
export const retry = async (operation: () => Promise<unknown>, options: any = {}) => { | |
const { retries, delay, onError = () => {} } = options; | |
return operation() | |
.catch((err) => { | |
if (retries > 0) { | |
onError(err, retries); | |
return wait(delay).then(() => retry(operation, { ...options, retries: retries - 1 })); | |
} else { | |
throw err; | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment