1. 16
    Convert a Promise.all Result to an Object with Ramda's zip and zipObj
    3m 11s

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

Andy Van Slaars
InstructorAndy Van Slaars

Share this video with your friends

Send Tweet
Published 5 years ago
Updated 3 years ago

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.

[00:00] 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.

[00:19] 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.

[00:38] 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.

[00:56] 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.

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

[01:31] 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.

[01:53] 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.

[02:07] 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.

[02:29] 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.

[02:49] 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.