What is a Promise?

Two men shaking hands
A handshake, the classic symbol of trust...or shall we say...a promise?

Much like how callback functions are used to write code that executes in the background, while other code executes in the foreground, promises also accomplishes this.

Promises were designed and developed in order to address the issues with callback functions when writing asynchronous code. Which, we will explore below.

Issues with Callbacks

While callbacks are great, and are still widely used today, they have many issues. These issues can be summarized in that they are too unpredictable and error-laden.

Many programmers become frustrated with callbacks due to how unpredictable the timing is. Sometimes the code is called too late, other times too early. Sometimes a customers credit card is charged once, other times more than once (uh oh!). Other times not at all (why!?).

To add to the programmer's woes, the triangle of doom (aka callback hell) will only further your anxiety when dealing with unmaintained code. It will be full of errors, and/or extremely hard to make any sense of it. This will in turn, cause you and your team to spend more time than needed on correcting any problems.

How then, can these issues be resolved, or at the very least be minimized?

Promises to the Rescue

All of those issues we previously mentioned about callbacks being unpredictable and buggy are solved by Promises.

Promises are a way for us to write our code in a way that is easier to read, and can still be read top-to-bottom without our eyes jumping up and down the page due to callback functions's triangle of doom/callback hell.

An example of Promise Use

Code demonstrating the use of Promises.
Javascript Promises in action

As you can see in the image above, they are read from top-to-bottom. Which is great for our eyes, and it also makes our code easier to understand.

How to use

  1. First, as we see in the image above, first we create our Promise using the "new Promise" syntax since Promises are objects.

    Then, we type in our logic for what we want to happen if what we want to happen succeeds or fails.

  2. Second, the result of that success or failure in step one, is then taken care of by our logic in the "then()" function.

  3. Third, we take care of any errors that occur with the "catch()" function.

  4. Fourth, is the optional step. We can then use the "finally()" function to ensure that whatever is within it, will always execute. It doesn't matter if there was an error or not, this code snippet will always execute at the end of our script.

Summary

As fascinating as promises are, they are well worth investing more of your time in order to further comprehend them. They are fantastic for writing asynchronous code, which is why they are used so much in Node.js.

They solve the unpredictable nature, and buggy mess that is callback function use.

Helpful Links

In-depth Study of Promises A great video to reinforce how to use Javascript Promises