I've been digging in RxJS operators recently and have finally seem to come to an understanding between the difference between exhaustMap and switchMap (of which I only ever use the latter one). What's not clear to me is that I don't really understand why I would ever want the previous emission to complete instead of the newest one.
Say with an API call, if I fetch some data based on a user request, I would only ever want to call the API with the latest values, right? Is exhaustMap better when the response of the API call isn't expected to change based on user action (like for example with multiple repeated clicks of the same button).
TL;DR - What are the use-cases for exhaustMap over switchMap?
For example when you working with http requests, If you divide HTTP call to command and query:
the use-case for exhaustMap is: when you want to send a command to the backend, you have to wait until the request has been finished, because the requests are asynchronous, if you send another action with the same param, you may get unexpected results from the backend, imagine if you send 2 delete request by same Id, after this object has been deleted:
so which message you will notify to the user?
second request will be drop by exhaustMap until the first one has been finished.
on the other hand, if you want a user information with userId-1. you ask the server with get method and userId-1 as param, what happens if you had miss click on user-1 link and immediately you clicked on the user-2 link to get the user 2 information, the use-case for switchMap:
so this is a use-case for switchMap, it will drop the first request (geting user 1) and replace the request with (get user 2). the backend also can drop the first request with cancelationToke to prevent unused query.