Friday, May 24, 2013

Understanding Q-Deferred in NodeJS through example

 var Q = require('q');  
 var step1 = function() {  
     var deferred = Q.defer();  
     setTimeout(function() {  
         console.log('step1');  
         deferred.resolve();  
     }, 800);  
     return deferred.promise;  
 }  
 var step2 = function() {  
     var deferred = Q.defer();  
     setTimeout(function() {  
         console.log('step2');  
         deferred.resolve();  
     }, 600);  
     return deferred.promise;  
 }  
 var step3 = function() {  
     var deferred = Q.defer();  
     setTimeout(function() {  
         console.log('step3');  
         deferred.resolve();  
     }, 400);  
     return deferred.promise;  
 }  
 var step4 = function() {  
     var deferred = Q.defer();  
     setTimeout(function() {  
         console.log('step4');  
         deferred.resolve();  
     }, 200);  
     return deferred.promise;  
 }  
 // for the following calls
   step1();  
   step2();  
   step3();  
   step4();  
 // output  
 // step4  
 // step3  
 // step2  
 // step1  
 step1().then(step2).then(step3).then(step4).done();  
 // output  
 // step1  
 // step2  
 // step3  
 // step4  

Wednesday, December 19, 2012

Why NodeJs is exciting?






If you are developing a JSP (or template) based web application then probably you would not consider node (except the fact that you're trying to improve scalability) as anyways you are developing a thin client. On the other hand if you'd like to develop an application with rich user interaction like using some JavaScript libraries, then the obvious problem would be the code base is split into Java and JavaScript.

I could see a few problems in this approach:

The first would obviously be caught fragmentation, wherein the objects you use on your client side are in JS and on the server side its Java. There is no reusability here. And you should also take care of serialization and the deserialization. 

Bring in node, the most exciting thing that comes to my mind right away is that you write code in JavaScript both on the client and server. Apart from this the greatest challenge for any Web developer is to support multiple devices. JS would be a natural choice as JS has become almost ubiquitous across platforms. 

Apart from that, the event driven programming model is another area which brings huge performance benefits in terms of scalability and responsiveness.

I'm planning to explore the possibility of utilising Dojo on NodeJs. This will seriously be an exciting area to explore and needless to say the performance benefits would be tremendous.

If you're going through the cycle of develope > compile > debug which every developer on earth does, NodeJs has extremely fast startup time compared to typical containers like Tomcat. 

If you're still wondering what kind of change NodeJs would bring in to the future of Web application, watch this presentation by LinkedIn engineer on NodeJs on the YouTube.




Happy Noding!