Flatten Nested Maybes with `chain`

Andy Van Slaars
InstructorAndy Van Slaars
Share this video with your friends

Social Share Links

Send Tweet

Sometimes, we run into situations where we end up with a Maybe within the context of another Maybe. Nested structures like this can get really confusing to work with. In this lesson, we’ll look at an example of this and see how chain can help us out by flattening the nested Maybes

Instructor: [00:00] In this file, we have a stub service function. In a real app, this would make a call to an API, but in this example, we'll just use this hard-coded result value as the API response.

[00:10] Our function uses the prop utility from Crox to return a maybe that wraps the result's body property. In our index.js, we import this get user function, and then we're calling it, and we're just running console.log on the then.

[00:24] If I save this file, open the terminal, I can run it, and we'll say that our promise results to a just of an object. The object that's wrapped in our just is the body property from our result.

[00:34] If we go back to the other file, we're going to see that we have a just of this object. What we want to do in our consuming code is actually just get the postal code for this user.

[00:46] We're going to come back over here, and I'm going to import a utility from Crox called Prop Path. We'll use require for this, and we'll grab crox/maybe/proppath.

[01:00] Then we'll come down here, and I'm going to add a then before our console log and our promise chain. I'm going to take the result that I get back, which is a just of our value.

[01:17] We're going to use map to invoke a function on it, and the function I want to invoke is going to be a call to Prop Path that's going to have our path predefined.

[01:26] I'm going to jump up here, and I'm going to create a new function, and I'll call it Get Postal Code. That's going to be a call to Prop Path, and we're going to define our path. We're going to leave the data argument off of it.

[01:41] We're just going to look for address, and then postal code. Now we can pass Get Postal Code into map. We'll save this, and back in our terminal, we'll run this again.

[01:58] You'll see this time we get our ZIP code back, but we get a just just and our ZIP code. What we've essentially done is taken our maybe. We've unwrapped the value.

[02:08] We've gotten a new maybe by running this call to Prop Path, and then we re-wrapped it in another maybe. Now we have a nested maybe.

[02:16] Working with nested types like this is going to get really cumbersome, especially if we start nesting even more levels deep. But this is not uncommon, so there's an easy solution for this.

[02:26] We can replace map with chain, and chain is essentially flatMap. It'll apply our function, but it'll also deal with flattening out that nested structure.

[02:37] If I save this, and I go back into the terminal and run this again, you'll see that this time, we're only getting one maybe rather than a maybe nested inside of another maybe.