RxJs Observables nested subscriptions?

Max Solid picture Max Solid · Mar 19, 2017 · Viewed 29.5k times · Source

Whats the way to simplify something like the following code example? I can't find the right operator.. could anyone give a short example?

this.returnsObservable1(...)
  .subscribe(

    success => {

      this.returnsObservable2(...)
        .subscribe(

          success => {

            this.returnsObservable3(...)
              .subscribe(

                success => {
                   ...
                },

Answer

user3743222 picture user3743222 · Mar 19, 2017

As mentioned in comments, you are looking for the flatMap operator.

You can find more details in previous answers :

Your example would read as :

this.returnsObservable1(...)
  .flatMap(success => this.returnsObservable2(...))
  .flatMap(success => this.returnsObservable3(...))
  .subscribe(success => {(...)});