Skip to content

Instantly share code, notes, and snippets.

@bullishgopher
Created October 15, 2020 03:14
Show Gist options
  • Save bullishgopher/7cb86c033b5fa182bcd932ef3c3f84d1 to your computer and use it in GitHub Desktop.
Save bullishgopher/7cb86c033b5fa182bcd932ef3c3f84d1 to your computer and use it in GitHub Desktop.
a debounce utility in TypeScript
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