The map() operator versus the flatMap() operator

These two operators are probably the most important and most used operators from functional reactive programming, and for this reason it is really important to know how to differentiate them.

The map() operator lets you pass as a parameter a function that receives as input the data from the observable and returns new data that will be propagated instead. This new data can be of any type. You can see the map() operator in the following example:

Rx.Observable   .of(1,2,3)   .map((i)=>i+1)   .subscribe((i)=>console.log(i)); 

This code is really simple and only sums one to each element of the observable and logs them to the console, so running it you will see this output:

    2    3    4

You can see a representation ...

Get Mastering Reactive JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.