A generic type that extends a function with any number of arguments and any return type.
The function to debounce.
The number of milliseconds to delay.
A new function that delays execution and returns a promise.
const expensiveOperation = async (value: string) => {
if (value === 'error') {
throw new Error('Operation failed');
}
console.log(`Operating on: ${value}`);
return value.toUpperCase();
};
const debouncedOperation = debounce(expensiveOperation, 500);
// This will resolve with "SUCCESS"
debouncedOperation('success').then(console.log);
// This will reject with an error
debouncedOperation('error').catch(console.error);
Creates a debounced function that delays invoking the provided function until after a certain number of milliseconds have passed since the last time it was invoked. The debounced function returns a promise that resolves with the result of the last invocation or rejects if the invocation throws an error.