Created
October 15, 2020 03:14
-
-
Save xgracias/7cb86c033b5fa182bcd932ef3c3f84d1 to your computer and use it in GitHub Desktop.
a debounce utility in TypeScript
This file contains hidden or 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
const debounce = <F extends (...args: any[]) => any>( | |
func: F, | |
waitFor: number, | |
) => { | |
let timeout: NodeJS.Timeout; | |
return (...args: Parameters<F>): Promise<ReturnType<F>> => | |
new Promise((resolve) => { | |
if (timeout) { | |
clearTimeout(timeout); | |
} | |
timeout = setTimeout(() => resolve(func(...args)), waitFor); | |
}); | |
}; | |
export default debounce; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment