Slobodan Blazeski,

As others have said, the idiomatic way to do this in node is to use the 
async library and to have callback functions that take an error ("err") as 
the first argument.

The reason why it's important to have each asynchronous function pass an 
error object to your callback is because it is impossible for the 
asynchronous operation to throw an error in such a way that your callback 
function can catch it, like you can with traditional, synchronous 
programming. For instance, if there is a database error or a calculation 
error, you probably want to short-circuit and display the error.

So, with your example modified to include error handling, it would look 
like this:

Object1.retrieveNum1Async(function(err, num1){
      if (err) throw err;
      Object2.retrieveNum2Async(function(err, num2){
            if (err) throw err;
            Object3.retrieveNum3Async(function(err, num3){            
                 if (err) throw err;
                 var result = (num1 + num2) /  num3;                  
            });
      });
});

You may not want to throw the error -- you may want to write it to the 
console or ignore it or pass it back to a caller further up the chain -- 
but in any case you'll want to do *something* with it and you'll probably 
want to short-circuit the operation at whatever point the error occurs. 
Handling errors at each level like this adds a lot of noise, so typically 
node developers turn to the async library, which allows you to consolidate 
error handling and callbacks by short-circuiting by default and calling a 
single callback with any errors from any of the async calls:

var async = require('async');
async.parallel({
    'num1': Object1.retrieveNum1Async.bind(Object1),
    'num2': Object2.retrieveNum2Async.bind(Object2),
    'num3': Object3.retrieveNum3Async.bind(Object3)
}, function(err, r) {
    var result = (r.num1 + r.num2) / r.num3;
});

One annoyance with this approach is the need to .bind() the methods to 
their respective objects because passing a method in Javascript effectively 
"disembodies" it from the object it was attached to. One way around this is 
to pass anonymous functions to async, as James Basco demonstrated above -- 
this way you're passing anonymous functions to async and you're able to run 
the methods right off of their corresponding objects.

Or, if you have the option, it can often be simpler and cleaner to write 
callback-heavy code by using closures and nested functions to maintain 
state, rather than the more traditional object-oriented style, due to the 
need to constantly bind methods. These two styles (object-oriented and 
closure-based) can be mixed and matched for optimum simplicity.

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to