Javascript Promise Pattern

Posted: February 11, 2013 in javascript

One of the things i usually talk about when referring to JavaScript programming is design and implementation pattern.

From all the JavaScript patterns out there one had been over spotlights lately because is very popular among Windows 8 HTML5 developer’s. One thing that many people don’t know is that the WinJS Windows 8 Promises are not more than an implementation of a JavaScript design pattern.

Promise Pattern can be very useful when programming in JavaScript  even if you are using JavaScript on the web. Basically a promise is an object that represents a future result (that’s way in many books some authors call them future), that will be obtained in a near future.

The promise pattern offer’s a way to use callbacks in a friendly and readable way. We can also see Promises as a more elegant way to do callbacks in JavaScript.

Let’s see an example:

operation1(params1) {

  async_task(params1, function() {
    operation2(params2) {
               async_task(params2, function() {
                   //do something
If you look carefully to the example you will see that you have a async chain call stack, this is a dummy example but if you imagine in some real scenarios this can become truly confuse, especially if you have some future need of extend the code. When thinking in an implementation based on Promise we are talking in a much clean way to solve the problem, next you have the pseudo code who solves the same problem we previously had.

function operation1(params1) {

  var promise = new Promise()
  async_task(params1, function() {
    // ...
  return promise;
function operation2(params2) {
  var promise = new Promise(this)
  async_task(params2, function() {
    // do something
  return promise;



The Promise pattern allow us to have a representation of a long runing process, instead of blocking and waiting for never ending loops. You can see the application of this pattern to JQwuery and other’s in



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s