Promises on Javascript

If you are programming javascript in a while. Or just started nodejs and are having some trouble with the famous callbacks hell that javascript provides. Well that's the price to use a lot a asynchronous functions. just like the code below

database.getUserProfile(function(profile, err){  
  if(!err){
    networkAdapter.getTwitterInfo(profile.twitterUrl, function(twitterData, err){
      if (!err){
        database.getFriendsFromTwitter(twitterData, function(friends, err){
          if(!err) {
            var friendsOnTwitter = friends.map(function(friend){
              return friend.userName;
            });
            sendResponse(friendsOnTwitter);
          }
        });
      }
    });
  }
});

There are up to 6 levels of indentation on that code. and it can potentially grown. This is because we are just doing 2 database calls and 1 network call that has to be treated in a synchronous way. That is something that sucks about javascript callbacks. Sure the code is not perfectly planned. But It's just an example of how it could have grown wrong doing just something that any system nowadays has to do. Which is handle multiple IO calls. For that problems like that Promises were created.

For an example of how handle promises I'm gonna use the Q library which is maintained currently by Kris Kowal. This library is highly used at many places that has a javascript interpreter, like nodejs and all the major web browsers.

To use the example above with it our code will became really more clear.

database.getUserProfile()  
  .then(function(profile){
    return networkAdapter.getTwitterInfo(profile.twitterUrl);
  }).then(function(twitterData){
    return database.getFriendsFromTwitter(twitterData)
  }).then(function(friends){
    var friendsOnTwitter = friends.map(function(friend){
      return friend.userName;
    });
    sendResponse(friendsOnTwitter);
  });

Just like that our code became so much more readable. With the then statement we pass a callback that will eventually be called if our expression executes successfully, in this case the IO calls. And we chain all the callbacks by returning a new promises that will perform another IO call that will call the next then callback in the stack.

But wait, what about the error handles. What happened to them?

They can be easily replace with just one last call at the end of the stack of promises handles, which is the catch call.

database.getUserProfile()  
  .then(function(profile){
    return networkAdapter.getTwitterInfo(profile.twitterUrl);
  }).then(function(twitterData){
    return database.getFriendsFromTwitter(twitterData)
  }).then(function(friends){
    var friendsOnTwitter = friends.map(function(friend){
      return friend.userName;
    });
    sendResponse(friendsOnTwitter);
  }).catch(function(err){
    console.log(err);
  });

Even the error handles became more clear to read, just like that. That's why I believe that the use of a Promise library can have a really good use. Especially now that the next itteration of the javascript standart will support Promises natively (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)

References:

Promisses/A spec http://wiki.commonjs.org/wiki/Promises/A
ES6 own implementation of promisses http://www.html5rocks.com/en/tutorials/es6/promises/