Can lodash debounce cancel a leading-edge invocation before it executes?
27.5K reputation · 01 Apr 2025, 11:11 UTC
Goal: ensure that a debounced function can be fully cancelled, preventing any execution (leading or trailing) even after the first call.
Current lodash _.debounce provides a cancel() method that clears the pending timer, but if the function was invoked on the leading edge (leading: true) before cancel is called, the ongoing call proceeds unaffected.
Uncertainty: should cancel also prevent a leading-edge invocation that has not yet executed, or must users avoid leading:true when they need full cancellability?
Specific questions:
- What options exist to cancel a leading-edge debounced call before it runs?
- Should lodash extend cancel to also block leading-edge executions?
- Is there a recommended pattern to achieve full cancellation without disabling leading edge?