Skip to content

Instantly share code, notes, and snippets.

@ValentaTomas
Last active April 26, 2022 13:38
Show Gist options
  • Save ValentaTomas/0a3478ef3baf68453a5d70fcb2e0da9d to your computer and use it in GitHub Desktop.
Save ValentaTomas/0a3478ef3baf68453a5d70fcb2e0da9d to your computer and use it in GitHub Desktop.
Make function synchronous
export type Procedure = (...args: any[]) => void;
function makeSync<F extends Procedure>(
func: F,
): (this: ThisParameterType<F>, ...args: Parameters<F>) => void {
let inProgress = false;
return async function(this: ThisParameterType<F>, ...args: Parameters<F>) {
const context = this;
if (inProgress) {
return;
}
inProgress = true;
try {
return func.apply(context, args);
} catch (error) {
throw error;
} finally {
inProgress = false;
}
}
}
export default makeSync;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment