From the course: Learning RxJS

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Switching to the latest request with SwitchMap

Switching to the latest request with SwitchMap

From the course: Learning RxJS

Switching to the latest request with SwitchMap

- [Narrator] Let's say our weather app wanted to make an API call to get the weather and then the humidity of any US state. It always feels like it rains in Washington, but maybe it'll change. How our app may work is by first getting the average temperature and then the average humidity. We can also offer getting the weather for multiple states and the user may click getting the weather for any of them. Because buttons are fun to click, the user can query multiple states at the same time. However, they may only be interested in the last state they asked for. When this happens, we need to stop making the pending request for the temperature or the weather. For our app, we should make sure the user only gets the data for the last state they asked for, even if they clicked getting the weather for other states before. In RxJS, we can make sure this happens using switchMap. This RxJS operator will cancel pending requests to…

Contents