What is a Promise and how it works?

blog-promise1

First of all, I promise to implement a promise in this article.

What is a Promise?

The Promise object is used for asynchronous computations. A Promise represents a value which may be available now, or in the future, or never.

MDN

Example!

In the example above, the function given to the promise ctor is executed async. Also it is determined to have 2 parameters. These are callbacks in case of success or fail. Promises should supply a success value when resolved or failure reason when rejected.

Don’t be intimidate by the arrow functions if you aren’t familiar with them
(parameter) => {/*action*/}
is equivalent to
function(parameter) {/*action*/}

Using the code above would be done by chaining the functions then and catch like that:

The function passed to then() will be invoked with an answer if the promise is or will be resolved. The function passed to catch() will be invoked with a reason if the promise will eventually reject. This way you can relate any async post-action to your code.

How promises work?

Well first, you have to keep’em 🙂

Let’s implement a very simple promise so we could have a better understanding on the basis of what is going on inside. Code followed with deep explanation:

First thing to notice is that when created a new instance of edge64Promise then given parameter action is immediately executed async. This is done by:

action is invoked with two private functions that edge64Promise delivers, resolve() and reject(). I’ll examine one of them, since both are really similar:

resolve() will be executed by the promise’ user when a promise resolves, hence it has an answer parameter. This answer is stored because it is not clear whether or not, at this point the then() is executed. If it isn’t, we are not really sure what to do with the answer and we must wait until then() is invoked. However, if then() is already invoked, then that.resolveCallback would be defined, and so we can call it.

Notice the use of that instead of this, I use it as a conversion in any class function/method in order to prevent context confusions which can happen a lot in JavaScript

At the moment when then() is invoked, we aren’t sure that the promise is resolved with an answer. If it is, we have two parts of the whole, the callback function and the answer to send to it, otherwise, we must store the callback function in case the promise will resolve.

We return that in order to allow chaining the catch() afterwards.

Note: I’ve explained the most plain and simple way of using promise. For instance, Notice that in my implementation you can’t chain .then() twice, and this option is important for other coding techniques which I will might post about in the future.

Summary

Promise will help you control an async code.

The trick in implementing a class like Promise is to realize that resolve() and then() relay on each other. resolve() misses the callback function and then() misses the actual answer. Same goes to reject and catch() but with reason, instead of answer.