Skip to content

Instantly share code, notes, and snippets.

@anthowen
Created June 29, 2020 12:12
Show Gist options
  • Save anthowen/23518df673851cd94b86754961f63790 to your computer and use it in GitHub Desktop.
Save anthowen/23518df673851cd94b86754961f63790 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