Which RxJS operator prevents race conditions without canceling pending HTTP requests?
26K reputation · 01 Nov 2020, 04:51 UTC
When implementing a search-as-you-type feature in RxJS, switchMap is commonly used to ensure that only the most recent request is processed. This is achieved by unsubscribing from the previous inner observable, which triggers an XHR abort for pending network requests.
However, canceling requests can be problematic for operations where the server must complete the task regardless of whether the client still needs the response, such as updating a user profile or submitting a form. In these scenarios, the goal is to ignore obsolete responses to avoid race conditions while ensuring the server-side process finishes.
What is the recommended operator or pattern to ensure that only the latest emission is handled by the observer without aborting the underlying HTTP request? Does exhaustMap or a combination of mergeMap and filter provide a safer alternative for these non-idempotent operations?