Encountered a bug related to debounced input and response handling.
A search input triggers server queries as you type with a debounce set to 300ms. However, there are instances of unexpected behavior:
If a user types "ab", waits 300ms, then types "c" before the initial request is resolved, they will see "abc" in the search bar but two network requests are made. Sometimes, the second request for "abc" resolves first, only to be overwritten by the results from the first request for "ab". This leads to displaying results for "ab" while the search input shows "abc".
This seems more like an issue of handling promises rather than just debounce functionality. There should be a way to discard older promises once new ones are made so they don't interfere with accurate responses.
Basically, what's desired:
- User types
"ab"
- Sends request "ab"
- User types
"c"
- Sends request "abc"
- Receives response for "abc", handles promise resolution
- Receives response for "ab", ignores promise
Are there any established patterns or approaches within Angular to tackle this issue? It seems like it would be a common challenge.
For example, something along the lines of "Resolving only the most recent created promise"?