Skip to content

Instantly share code, notes, and snippets.

@christopherbauer
Created April 30, 2023 23:21
Show Gist options
  • Save christopherbauer/510b3df74a2b03233b73eb22df2c3a3a to your computer and use it in GitHub Desktop.
Save christopherbauer/510b3df74a2b03233b73eb22df2c3a3a to your computer and use it in GitHub Desktop.
export const Retry = (count: number, waitMS: number) => {
return function repeater(
originalMethod: Function,
_context: ClassMethodDecoratorContext
) {
function ReplacementMethod(this: any, ...args: any[]) {
let tries = 0;
const attempt = () => {
tries++;
try {
originalMethod.call(this, ...args);
} catch (err) {
if (tries < count) {
setTimeout(attempt, waitMS);
}
}
};
return attempt();
}
return ReplacementMethod;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment