0

I'm ok with javascript and callbacks, but I'm getting really annoyed at this and need to call on the the world of stackoverflow for help.

I have written a function, to be used in the following way:

var meth = lib.funcTion(a,b); // meth should hold an array of properties { c, d } once executed

So now inside lib.js, we have a structure like:

exports.funcTion = function (a,b) {
    database.connect(params, function(err,get){
          get.query(querylang, function(err, results){
                 var varsIwantToReturn = { var1: results[i].foo, var2: results[i].bar };
          });
    });
// Now how do i return 'varsIwantToReturn'?
};

I have seen some things about incorporating callback() into the function, but I'm not exactly sure how this works. I've also seen some people use exec() - again, im not sure on how or why to use it.

Please help :) thanks in advance.

sidewaiise
  • 1,445
  • 3
  • 16
  • 27

1 Answers1

1

Well, it's all asynchronous so if you attempt to return it - it'll return undefined. In JavaScript (Sans the new yield keyword) functions execute from top to bottom synchronously. When you make an IO call like a database call - it still executes synchronously. In fact- when varIwantToReturn gets population the function has long run and terminated.

What is left is to do the same thing async functions like database.connect and get.query do and have the function take a callback:

exports.function = function (a,b, callback) {
    database.connect(params, function(err,get){
          if(err) return callback(err, null); // don't suppress errors
          get.query(querylang, function(err, results){
               if(err) return callback(err, null); // don't suppress errors
               var varsIwantToReturn = { var1: results[i].foo, var2: results[i].bar };
               callback(null, varsIwantToReturn);
         });
    });
};

Then you'd call it like

myExportedFunction(myA,myB, function(err, resp){
    if(err) recoverFromError(err);
    // varsIWantToReturn are contained in `resp`
});
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • I made this CW since this is probably a duplicate. Feel free to edit. – Benjamin Gruenbaum Aug 18 '14 at 06:10
  • Thanks for the fast response Ben, will give this a go. As you've explained it, makes perfect sense though. BBS – sidewaiise Aug 18 '14 at 06:26
  • @user2869420 check out http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call it's not the same question but it's about the same problem – Benjamin Gruenbaum Aug 18 '14 at 06:27
  • Just one last question, what is the significance of the 'null' in the callback()? – sidewaiise Aug 18 '14 at 06:37
  • @user2869420 it is the NodeJS "errback" convention - it's what Node uses in its APIs to overcome the lack of asynchronous exceptions - the callback function must resolve _exactly once_ and must have either an error set and a response be null or vice versa. It's a convention to help you work with other peoples' code. Personally I'd rather use a stronger abstraction that doesn't require nesting or silly null parameters like promises - but if you're using callbacks it is desirable to use the convention. – Benjamin Gruenbaum Aug 18 '14 at 06:39
  • Ok thanks Ben. If you have a method which you think would be better, open to hear it. I've only been using nodejs for a short period and this way seemed most practical. Cheers. – sidewaiise Aug 18 '14 at 06:41
  • @user2869420 Promises! I personally use Bluebird, see the examples here https://github.com/petkaantonov/bluebird – Benjamin Gruenbaum Aug 18 '14 at 06:42
  • Great, I'll give it a go, thanks :) yet another nodejs library to investigate! Cheers – sidewaiise Aug 18 '14 at 06:47