This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Use RxJS switchMap to map and flatten higher order observables

6:04 RxJS lesson by

In this lesson, we will learn about switchMap, a shortcut for mapping to a higher order RxJS Observable, then flattening with switch. We will also see a practical example with network requests that would be used in the real world.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

In this lesson, we will learn about switchMap, a shortcut for mapping to a higher order RxJS Observable, then flattening with switch. We will also see a practical example with network requests that would be used in the real world.

Avatar
HaveF

Very interesting when click twice, it will cancel the first fetch process. But how does it happy exactly? fetch normally does not have cancel method( javascript - How do I cancel an HTTP fetch() request? - Stack Overflow ), and promise's cancel method is just implemented in some library like bluebird, so how it happens? Thanks for your video, Andre!

Avatar
Andre

Hi HaveF. You are right that Promises have no cancellation mechanisms. When we use them in observables, their results would simply be ignored on unsubscribe, not actually cancelled. For this reason, RxJS also has its own ajax methods that internally use XMLHttpRequest, to support cancellation. We use them like Observable.ajax('http://jsonplaceholder.typicode.com/users/1') which returns an observable of response objects.

In reply to HaveF
Avatar
HaveF

Thanks for your explanation +1

In reply to Andre
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?