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 Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

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