6

MergeMap in Angular | MergeAll and Map

What is MergeMap in Angular?
MergeMap essentially is a combination of mergeAll and map. MergeAll takes care of subscribing to the 'inner' Observable so that we no longer have to Subscribe two times as mergeAll merges the value of the 'inner' Observable into the 'outer' Observable.

The mergeMap() : Projects each source value to an Observable which is merged in the output Observable — (Official RxJS Docs)

Why use mergeMap?
This operator is best used when you wish to flatten an inner observable but want to manually control the number of inner subscriptions.

Explore in detail about Angular FAQs

Example 1,
import { tapmergeMap } from 'rxjs/operators'

sourceObservable.pipe(
  tap(e => ...),
  mergeMap(e => ...)
)

Example 2,
getItems(idsnumber[]): Observable<Item> {
  return from(ids).pipe(
    mergeMap(id => <Observable<Item>> this.httpClient.get(`item/${id}`))
  );
}

ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

www.code-sample.com/. Powered by Blogger.
^