Send new requests from refresh clicks in RxJS

André Staltz
InstructorAndré Staltz

Share this video with your friends

Send Tweet

In this lesson we learn how to fetch new data whenever the refresh button is clicked. This lessons starts to give us a feeling of how is it to develop reactive applications in RxJS.

Joshua
Joshua
~ 5 years ago

Why do you need flatMap in the responseStream?

André Staltz
André Staltzinstructor
~ 5 years ago

Hi Joshua, if we try to simply map a request URL to a response, we will get a stream of streams, because each response is a stream (since a response always comes asynchronously in the future). With a stream of streams, we can "flatten" it by using mergeAll. So map + mergeAll is what we would need to do. Actually, flatMap is precisely map + mergeAll. That's why we use flatMap.

JL decentraliser
JL decentraliser
~ 2 years ago

OMG please update to RXJS 6, took so long for me to find how to use merge :'( for those who are like me (I use node-fetch):

var responseStream = merge(startupRequestStream, requestOnRefreshStream). pipe( flatMap(requestUrl => from(fetch(requestUrl).then(res => res.json())) ) );