MergeMap vs ConcatMap Vs ForkJoin

Sumit kumar Singh
3 min readMar 30, 2023

mergeMap, concatMap, and forkJoin are operators in RxJS, a library for reactive programming in JavaScript. These operators are used to transform streams of data emitted by Observables.

MergeMap

mergeMap is an operator that transforms an Observable's emissions into Observables, then flattens all of the resulting Observables into a single stream of data.

const { fromEvent } = Rx;
const { mergeMap } = RxOperators;

const clicks$ = fromEvent(document, 'click');
const request$ = clicks$.pipe(
mergeMap(() => fetch('https://jsonplaceholder.typicode.com/users'))
);

request$.subscribe(response => {
console.log(response);
});

In the above example, mergeMap is used to transform the clicks$ Observable into an Observable that makes an HTTP request to the JSONPlaceholder API for a list of users. When the user clicks anywhere on the page, mergeMap flattens the resulting Observable returned by the fetch function into a single stream of data that emits the HTTP response.

ConcatMap

concatMap is an operator that transforms an Observable's emissions into Observables, then concatenates all of the resulting Observables into a single stream of data.

const { fromEvent } = Rx;
const { concatMap } = RxOperators;

const clicks$ =…

--

--

Sumit kumar Singh

YouTube: https://www.youtube.com/@tech..Design/ 📚 HTML,Angular, React,and JavaScript 🧑‍💻 Tips & tricks on Web Developing 👉 FULL STACK DEVELOPER