The Sin of Certainty

For many people in our world, the opposite of faith is doubt. The goal, then, within this understanding, is to eliminate doubt. But faith and doubt aren’t opposites. Doubt is often a sign that your…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Maybe a Promise?

The more I use Promises the more I like it, it’s so flexible and easy, it’s not flawless, but yet very powerful. How much do you know about Promises? Have you ever thought about other Promises applications besides using it on ajax calls?

I recently posted a case that you can use a promise to solve some tracking asynchronous issues, and in this post I’ll show another case where a Promise can be used to help you to write safer code.

If you are a Javascript developer then you saw that message countless times. Functional Programming is the big thing in these days, and there are some technics you can get from this paradigm in order to mitigate runtime errors like that, especially when you have to deal with data that you don't have much control and can't be sure about the absence of a value.

But you have to wait until this feature is available in the language if approved. If you don't want to load your application with FP libraries and you're worried about the learning curve of Functional Programming heavy concepts, well… maybe there's a simpler way.

We’ll borrow the Maybe abstraction from FP paradigm, but we are not following the same principles and laws, we just gonna use the idea.

In our implementation, Maybe is just a container, a box, that wraps the data and hides the value from you, we have to use an interface or a function that will be executed if the value is not null or undefined , that way we can prevent ourselves to work directly with undefined values:

The code above will be executed only if the Maybe value is not undefined or null , so we can affirm that number exists in the arrow function body.

So, let's build our Maybe Promise :

You can pass to Maybe any value and it will always return a Promise. So you can use the Promise to branch your logic, if there's a value, you can unwrap and use it with .then() otherwise you use .catch if the value is null or undefined.

You can also branch the logic only using.then() :

There's a special case in our Maybe implementation, we can pass a function to it, and that's what let us to safely access some property in a complex data:

The code above can fail if data or personsFirstName is null, but it's ok because Maybe handles it internally with a try/catch, so we just branch it as we do in the other cases:

Consider the following data structure:

Let's say that we have this odd data structure, and we want to form a full name string using our Maybe abstraction, also, we wanna set a default value for each part of the name in case of null.

The full name could be assembled this way:

We have the unwrapped value in the .then callback, that's a pretty handy property of the Promises.

To check if the default value is working, we can change the index of the array of personsLastName to 1 and force an error.

In the examples above we are setting default values in the error cases, so we'll never get into the catch case, because our code will always fallback to default values.

In order to break the then's executions whenever an error occurs, we have to change our code a little:

Instead of returning a string in the catch function, we just return a rejected promise. This will cause a break on the then chain, the error can be caught later:

I'm not saying that you don't need to learn about FP or use libraries that can help you with issues like dealing with null/undefined values. I just wanted to show another way to use promises.

Promises have some interesting properties and I wonder if we exhaust all the possibilities with it, the more I work with it the more I realize that there's still more to learn about it.

It might not be enough for several cases, but it's possible that it's all you need to deal with the most of your daily issues regarding handling with uncertain values.

That's all I got for you guys, thanks for reading.

Add a comment

Related posts:

The Swing Chair on the Porch

Thanksgiving can be a nightmare. My marriage certainly was, and I couldn’t do anything about it. All I needed was my sexist brother-in-law hounding me, my in-laws judging me. I just want to be happy…

A Warmer Cold Call

Teachers cold-calling on students serves two purposes: the student responds and the class (maybe) feels they too could be called upon. Some teachers might intentionally call on inattentive students…

In Praise of Being Bored

Boredom is a state of mind that makes most people horribly uncomfortable. When all occupations temporarily leave us, and we’re left floundering alone with our thoughts, we might bear witness to a…