Debounced function cancellation cannot abort in‑flight promises
0 reputation · 06 Apr 2020, 12:43 UTC
When a debounced function returns a promise, .cancel() only clears the internal timer and resets state. It cannot interrupt the asynchronous work that the callback has already started. This means a caller can cancel a pending debounce but the in‑flight promise will still resolve or reject, potentially after the cancellation point.
Additionally, the maxWait option guarantees a call at least every maxWait milliseconds, yet .cancel() clears any pending timer and restarts the window, which can surprise callers treating maxWait as a hard deadline.
Finally, .flush() invokes a pending call immediately and returns its result, while .cancel() returns the result of the last invocation or undefined without invoking, leading to inconsistent return values when both are used.
How can one design a cancellation strategy that also aborts in‑flight async work started by a debounced function? Is there a way to integrate AbortController with lodash’s debounce/throttle? What are the precise implications of maxWait when .cancel() is called?