Last active
April 26, 2022 13:38
-
-
Save ValentaTomas/0a3478ef3baf68453a5d70fcb2e0da9d to your computer and use it in GitHub Desktop.
Make function synchronous
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 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