Problem Statement
In complex applications, we need the ability to cancel pending debounced operations—for example, when a component unmounts or when the user navigates away. Implement a debounce function with a cancel capability.
Requirements
Implement a debounce(func, wait) function that returns a debounced function with:
- A
cancel()method that cancels any pending invocation - Standard debounce behavior (trailing edge)
- The cancel method should be safe to call multiple times
Example Usage
Follow-up Questions
- How would you implement a
pending()method to check if there's a pending call? - Should cancel return anything? What would be useful?