Using the switchMap and debounceTime operators with autocompletes for better performance
For a lot of apps, we have features such as searching content as the user types. This is a really good user experience (UX) as the user doesn't have to press a button to do a search. However, if we send a call to the server on every key press, that's going to result in a lot of HTTP calls being sent, and we can't know which HTTP call will complete first; thus, we can't be sure if we will have the correct data shown on the view or not. In this recipe, you'll learn to use the switchMap
operator to cancel out the last subscription and create a new one instead. This would result in canceling previous calls and keeping only one call—the last one. And we'll use the debounceTime
operator to wait for the input to be idle before it even tries to make one call.
Getting ready
The app that we are going to work with resides in start/apps/chapter05/rx-switchmap-operator
inside...