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 832 of the free egghead.io lessons, plus get JavaScript content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Just one more step!

Check your inbox for an email from us and click link to unlock your lesson.



Use chain for composable error handling with nested Eithers

3:57 JavaScript lesson by

We refactor a function that uses try/catch to a single composed expression using Either. We then introduce the chain function to deal with nested Eithers resulting from two try/catch calls.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

We refactor a function that uses try/catch to a single composed expression using Either. We then introduce the chain function to deal with nested Eithers resulting from two try/catch calls.

Avatar
Brian Lonsdorf

I'm not sure I understand your question. Can you clarify? Either is the actual monad here chain is >>=

Let's look at another example of using Either. We have our previous definitions here, right and left and front nodable. I've written a little get port function we're going to refactor using FS. What does this do? It's going to read the file, config.json, which I've prepared here, config.json. It's got port 888. It's going to parse it, and then grab the port.

If anything goes wrong we'll return 3,000 as a default. If I run this, we should have ourselves an 888. There we are. If we mess up the file name we will get the 3,000 default just like that.

The first thing we need to do here is let's go ahead and copy this as a template, and grab our first line. You'll notice it's in a try catch. We're going to use Either to get rid of this try catch. Let's go ahead and write one once and for all with a little helper method here. We'll call it try catch.

We'll take a function and say if the function succeeds, we will put it in a right. If it fails in the catch case, we'll throw it in a left here, return the left of e. That is all there is to it. This is the one and one time only we'll write try catch. Oh, I forgot the T here.

If it returns, we'll know if it's a right or a left. We don't have to check if it's an error or not, or do another try/catch somewhere else, because we have a composable type that will do the right thing when mapped and folded.

Let's go ahead and use this try catch down here to wrap our read file. That way it won't explode if it doesn't find the file. Then we can map over that if it succeeds it will run this map. If it returns our left, remember, it will not run map at all. We can count on json parse working here because we have our contents.

Then we will fold it down. If we have an error we'll return our 3,000 default. If we get our parsed config here we'll just do port on that. This should work just fine. Let's go ahead and comment out the old one, and try this out and take it for a whirl.

There we are, 888. Good times. If we get rid of this I or G or something and we run it again, we get 3,000 just like we wanted. But there's one thing going on here that the old one did. This one doesn't. If you look up here it's all in a try catch block. If we go ahead and remove the port 888 and we try to parse this, what's going to happen is this parse is going to explode in it.

Then we're not going to have a value. Let's go ahead and give this a shot. It will blow up just like we expected. Unexpected end of json input. What we have to do here is put another try catch just like that. That will capture the error.

However this map is going to open up our right here. Say we have a right of some contents. It's going to pass in the contents. Then try catch returns another Either. We have a right of either a left of some error, or we have a right of another right.

Just like box in the other example, we'll have to fold twice. This gets really, really confusing. What we're going to do is write a new function that's just like map. We're going to call it chain. That will return one right.

Let's head up and define chain. Chain is just like map, except we're not going to box it back up just like we wanted. That way we'll end up with one right or left after the end of this. Then similarly with our left side, we want left to act like a left. Left just ignores everything. That's all we need to do is return itself back.

Don't worry if you're getting confused about all these definitions. There's only going to be a handful. It's more about how we use them in context. We'll develop an intuition for these things.

Let's go ahead and use this chain here. It says, "If we're going to return another Either, we are going to use chain instead of map." Let's go ahead and run this. There we are. We get 3,000 because our config is malformed.

If we remove this part, we still get 3,000. If we go fix our config.json back to where it was, we will get our 888 just like that. Very good. Is there any questions?

Isn't chaining and folding the same thing sometimes?

Right. Chain is defined just like boxes fold was. The idea is that here, fold is going to capture the idea of removing a value from its context -- taking it out of the box, whether it's a right or a left or a box itself.

Chain expects you to run a function and return another one. We'll keep that convention and intuition as we go along. They are two very functions even though they might have the same definitions sometimes.

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