I recently came across an enlightening article on RXJS which delves into the concept of flatMap
.
After understanding its purpose - to flatten observable of observables into a single observable sequence (similar to SelectMany
in C#) - I noticed an interesting use case where flatMap
was employed for a jQuery getJson
request:
var responseStream = requestStream
.flatMap(function(requestUrl) {
return Rx.Observable.fromPromise(jQuery.getJSON(requestUrl));
});
While this usage is common and makes sense, it got me thinking, could it be achieved with a simpler approach?: (The code below doesn't function correctly, but it seems like it should logically work.)
var responseStream = requestStream
.map(function(requestUrl) {
return jQuery.getJSON(requestUrl);
});
?
Question
What exactly sets apart these two code snippets?