What is MergeMap Operator - RXJS

What is MergeMap Operator - RXJS

In previous article we have seen RxJS concatMap Applying Observable concatenation to a series of HTTP operations seems like a good way to ensure that the HTTP oparation happen in the intended order.

However, in some situations, we want to run things in parallel, that is why we've got the Strategy merge Observable!

So Unlike concat, Merge will not wait until the Observable finishes until the next Observable is subscribed.

What is MergeMap Operator - RXJS

MergeMap is an flatting operator

Map + mergeAll = mergeMap

const source = from(["Tech", "Comedy", "News"]);

source.pipe(mergeMap((res) => this.getData(res))).subscribe((res) => {
      console.log(res);

    });

 getData(data) {
    return of(data + " Video Uploaded");
  }

Will output:

Tech Video Uploaded
merge-map.component.ts:36 Comedy Video Uploaded
merge-map.component.ts:36 News Video Uploaded

Subscribe to our Newsletter

Stay up to date! Get all the latest posts delivered straight to your inbox.

If You Appreciate What We Do Here On TutsCoder, You Should Consider:

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

You might also like

Leave a Comment