Running multiple requests in parallel

This is a common pattern we rely on, since most of the data we need from our backend is across multiple endpoints (although we’ve launched a GraphQL beta 😃) and we don’t care which request finishes first.

Note, this is different from the recipe in the docs, which describes waiting for each request to complete before being able to access any of the responses (like Promise.all ). The following pattern allows you to start many requests at once, but they are all individual tasks that can finish at any time.

This pattern can be a little bit hard to digest at first glance; lets break it down a bit:

First we have a watcher ( onPageInit ) that will fire our saga ( fetchDataForPage ) any time the ON_PAGE_MOUNT action type is seen. In the fetchDataForPage saga, we first gather any dependencies we need to make our requests (userIds, etc) Using redux-saga’s all and fork APIs, we start as many requests as we need in parallel. We pass our reusable requestAndPut side effect function, along with the actual request function we want to call eg: firstRequest , any parameters we need, and finally the action creator we want to call if the request succeeds. If any of the requests fail, we can use the catch block for our error handling. Else, the action creators will dispatch our success actions, leaving it up to our reducers to handle these and update our store accordingly.