Javascript: promises

Posted by in Javascript and TypeScript

Since few months ago, there is a feature related to Javascript that I must include as a new resource in my programming skills, and of course, migrated to TypeScript. To be clear, I am talking about promises. Promises are here to stay, and the reaseon is becuase promises are designed for a better writting software development. So, that’s my reason to write this entrance, and even more if you use AJAX calls.

What is AJAX

AJAX are a set on tecniques that allow a developer to change data from a server without reloading the full page. Well, that’s a very short explanation, but you can go further by reading this book (in spanish): http://librosweb.es/libro/ajax/capitulo_1.html, a book a already read a few years ago when trying to understand what’s going on behind. Of course, learning AJAX calls took me some time, until I used jQuery and in that moment, you think how lucky you are because another deveopers had the same problem as you before!

jQuery appear in my programming life to free me a lot of pressure because I couldn’t understand (well, some times I still have the same sensation) how Javascript and callbacks works:

Callbacks are functions that are excecuted after other functions ends.

At the end, you can understand the game of joining callbacks and how they work, but the Javascript code is almos a totally mess. Here it is very well explained.

The time (and projects) has passsed and I learnt Javascript becuase of jQuery, but now I move to TypeScript (TS) (I don’t code in Javascript anymore), where the rules are more stricted and that feature has allow me to be a better Javascript developer (well, typescript developer!).

Javascript: Promises

When you work with single page applications it is compulsory to use AJAX, jQuery and callbacks in order to created good apps with good performance, specially now that we have so good browsers (not only for diving into Internet but also for programming). Javasceipr Promises is a feature created in ECMAScript 2015, ES6:

The object Promise is used for deferred and asyncronous computations, and it can only be in one of these four states: pending, fulfilled,rejected or settled.

Click in this link for a further reading on Promises: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise (and here in  spanish).

The main reason for using promises is error handleling, because promises can be fulfilled or not (there are no more options), and depende on that, one piece of code will be run, or another code. This is the key for developing better Javascript apps (or TS apps, in my case), necause callbacks are dependant of several factors and you never can control allo of them (as an example, when you are using an API o a third party company), os a callback calls another callback, this second callbacks calls a thid callback and so no.

The second reason for using promises are when debugging code.

The third reason is that you can create a chain of promises.

If you are not convince of using promises, … is can be a good idea to follow the course yo can find in the next lines below.

Sintaxis of promesas

The sintaxis of promises is not complicated:

new Promise(function (resolve, reject) {
  var value = dosomething();
 if (condition) { resolve(value);   }
else {  reject(value);  }
} )
.then(function (value){
  // executed
})
.catch(function (value){
 // fails
})

Not bad for all the benefits I already told ou.

Course of Promises in Javascript

To end this entrance, I would like to mention that you can find a cool course Promises in javascript in udacity.com.

Happy coding.

Google+ Comments - Comentarios Google+