⚠️ This lesson is retired and might contain outdated information.

Combine Observables in Angular 2 with flatMap

Share this video with your friends

Social Share Links

Send Tweet
Published 8 years ago
Updated 9 months ago

In this video you'll learn how to combine multiple Observable streams using the flatMap operator

[00:00] We're doing pretty good with our instant search, but there are still a couple of things left to tackle such as dealing with out of order responses. While I prefer we dive into that, let's first improve ergonomics of our code to already pave the way for the more advanced stuff.

[00:13] Notice that we have one subscribed call here, and when it's called, it invokes the search method on our component. Then we have the search method itself, which makes the service call, and subscribes to the results. Basically, we have two subscribed calls in our code which are loosely connected via a method call.

[00:29] Since observables are all about composibility, these things are often the code's mouth. Imagine we would simple map on the current on the search term, their service call which returns an observable of a ray of string. We can totally do that, and we end up with an observable of observable of a ray of string, which can be a bit of a brain twister at first.

[00:48] Now, when we subscribe here, the payload is the observable that our service call returned which means we still have to subscribe within our observable to get through the actual area of strings that we are interested in. We give it a function, and say this.items equals results. Now, we should remove the search method from our components since we don't use it anymore.

[01:07] When we turn to our browser, we will notice that everything still works as expected. However, we still have two subscribed calls, because our payload became an observable itself. Luckily, there's an operator called match map, or flatMap which lets us map to another observable.

[01:24] Instead of our simple map which turns your observable into an observable of observable, when you do that, flatMap automatically subscribes to these inner observables, and flattens them into just one observable of the same type.

[01:39] Notice that we have to write the import as match map since flatMap is simply an alias to match map. All we have to do now is to change map to flatMap, or match map if you prefer. Since flatMap flattens for us behind the scenes, we don't have an observable of observable anymore.

[01:54] Which means we can change the call back of our subscribe because it now receives the plane area of terms instead of an observable that we manually have to subscribe to. Once again, we turn to our browser, and we see that everything works just like before, but the code is a lot cleaner now.

egghead
egghead

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today