Skip to content

Instantly share code, notes, and snippets.

@kabukki
Last active February 25, 2022 14:47
Show Gist options
  • Save kabukki/f6a18b0cc7cc8116629f5b9f0c8f7462 to your computer and use it in GitHub Desktop.
Save kabukki/f6a18b0cc7cc8116629f5b9f0c8f7462 to your computer and use it in GitHub Desktop.
TS/JS helpers
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