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.

1 Answer 1

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`
});
Sign up to request clarification or add additional context in comments.

8 Comments

I made this CW since this is probably a duplicate. Feel free to edit.
Thanks for the fast response Ben, will give this a go. As you've explained it, makes perfect sense though. BBS
@user2869420 check out stackoverflow.com/questions/14220321/… it's not the same question but it's about the same problem
Just one last question, what is the significance of the 'null' in the callback()?
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.