Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Create multiple Ajax requests and group the the results.

    redux-observableredux-observable

    With redux-observable, we have the power of RxJS at our disposal - this means tasks that would otherwise be complicated and imperative, become simple and declarative. In this lesson we’ll respond to an action by queuing up 2 separate Ajax requests that will execute in parallel. Then we’ll group the results from both into an array and produce a single action from our epic that will save the data into the redux store.

    Code

    Code

    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: To demonstrate how easy it is to compose multiple asynchronous computations with RxJS, let's re-purpose this application so that instead of a search term, we're just going to return random results based on how many are selected here. We're going to replace this import with a button instead, but first we need to create an action.

    We'll copy search and we'll just call it random. It won't need any input. Now that we have that constant and action creator, we can use this in the UI. We go back to the component and we'll replace this input with a button instead. It's type button and the on click handler will call the random function.

    We'll replace search here and we'll need to bring in the action creator below here. We'll just import that. Now you can see we have this random button. If we click it, you can see we get an action dispatched into the store and no results because we need to wire it up inside our epic.

    We can re-purpose this fetch beers epic. We'll add another helper function for the API URLs. The particular API that we're using supports this endpoint random. It just gives us back a single beer. Because we're allowing the user to select how many random beers we want, we'll have to compose together multiple AJAX requests. The first thing we need to do that is the per page configuration.

    We can say requests is a new array. We use the spread trick to create an array of a certain length. We'll say config.per_page. If the user selected five, this will become an array of five elements. We don't care about the elements, we just want to map that into five different AJAX requests. We can change this for the random helper and we don't need the second two arguments.

    Now this is going to be a plain array that inside it contains a number of observables that are not making AJAX requests yet. They're just sitting there waiting to be subscribed to. Then we just need something from Rx that can take this array of observables, subscribe to them all, and give us the results back in one array. For that we can use fork join.

    We mustn't forget to change search here to random and import that constant. We also won't need this filter anymore since we're not using the text input from the user. Let's check that in the browser. We can change this to three results, hit random and see what happens. You can see we have an error here. We made three API requests.

    If we look, each API request, even though we know it's only a single beer, it's given us back that single item in an array. The problem here is that fetch fulfilled action creator here expects beers as a flat array. What we end up sending in is a nested array. We can prove that if we just put console log and run the experiment again.

    You can see before the error we have an array of three elements and each element is itself an array. We can solve that problem before it gets anywhere near the reducer by changing what we return from this getJSON. Because that is just an observable, we can use the pluck operator to access the first element.

    This means that every observable from here will just return the inner beer from the response, it won't return the outer array. With that simple change there, we can try again. There you go, we make two separate API requests to the same URL. We group the results, and we showed them all together at once.