Send new requests from refresh clicks in RxJS

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
~ 9 years ago

Why do you need flatMap in the responseStream?

André Staltz
André Staltz(instructor)
~ 9 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
~ 6 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())) ) );