Rxjs One Observable Feeding into Another

For transforming items emitted by an Observable into another Observable, you probably want to use the flatMap operator. It creates an inner Observable and flats its result to the outer stream.

const source = this.myService
    .getFoo()
    .pipe(
        flatMap(result => this.myService.getMoo(result))
     )
    .subscribe(result2 => {
        // do some stuff
    });

Here are some flat operators you can use that behave differently:

  • flatMap/mergeMap – creates an Observable immediately for any source item, all previous Observables are kept alive
  • concatMap – waits for the previous Observable to complete before creating the next one
  • switchMap – for any source item, completes the previous Observable and immediately creates the next one
  • exhaustMap – map to inner observable, ignore other values until that observable completes

Leave a Comment