Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 1047 of the free egghead.io lessons, plus get JavaScript content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Convert a Promise.all Result to an Object with Ramda's zip and zipObj

3:11 JavaScript lesson by

In this lesson, we'll use Promise.all to get an array that contains the resolved values from multiple promises. Then we'll see how we can use Ramda to convert that array of values into a single object using zip with fromPairs. Then we'll refactor to use zipObj.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

In this lesson, we'll use Promise.all to get an array that contains the resolved values from multiple promises. Then we'll see how we can use Ramda to convert that array of values into a single object using zip with fromPairs. Then we'll refactor to use zipObj.

Here I have two functions that return promises. Get Name which immediately resolves to my name and Get Hobbies where I'm using a set timeout to create an artificial delay of half a second. That resolves to an array of hobbies. I'm calling both functions inside of an array using promise.all to resolve the results and log them out.

When I run this, you'll see that I get back an array for the resolve value of my first promise as the first element and the second element is the array that the second function resolves to. What I'd really like to do is get an object that I can work with that has the name property and a hobbies property. I'm going to update my promise chain and add a then.

In that then I'm going to take a function that accepts my result, which is my array, and returns an object with name and hobbies properties. I give it name and that's going to be my result of zero, and hobbies which is going to be my result of one.

This works, but I really don't like having the indices mentioned in this object. What I can do is I can come back here and instead of just taking the response as an array I can use destructuring and I can actually pull those array values in as individual items. I'll pull them in as name and hobbies, and then I can update my object that's being returned to use name and hobbies.

I can shorten this even further by using shorthand syntax here since my names and the values are the same.

In order to tighten this up even further I'm going to pull in some functions from Ramda. Ramda is already included in my file here, so I'm just going to use destructuring to grab a couple utility functions out of it. I'm going to grab zip and from pairs and I'll just set that to equal R. Now we can come down here and I'm going to update this then.

I'm going to get rid of this object. I'm going to enclose both of these key names in quotes, and then I'm going to add a call to zip.

What we'll see is the output has changed and I've lost my object, but it's created an array of arrays. Each of those inner arrays is a key in a value pair. You can see that I have a name and hobbies. Now what I can do is I can add to my promise chain with then and I can use Ramda's from pairs to convert that back into an object.

Now that I have these two steps, I'm looking at it and I'm saying, "You know what? I can compose these." I'm going to create a composition. I'll call from pairs after my call to zip. I can get rid of that second then. Now I have my object again.

This composition of zip and from pairs is so common that there's actually a shorthand for it. I'm going to get rid of the call to compose from pairs. I'm going to use zip object from Ramda. I can replace all of these imports with a single zip object and we're back to our original result.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?