0

I am relatively new to javascript and working on nodejs. I have a situation if reduced woulf boil down to following code. Can I return directly in the callback with out creating another variable(temp) like I did in the below code.

exports.run = function(req, res) {

    var testRunId = req.body.testRunId;
    var testFileDir = './uploads/temptestfiles/'+ testRunId + '/';

    var error = null
    var status ;

    mkpath(testFileDir, function (err) {

        if (!err) {
            error = {error:'could not create logs directory'}
            status = 500
        };

    });

    if (status === 500) {
        return res.send(500, error);
    }

//handle many more cases

}

Following is dumbed down version.

var a = function(param,callback) {
    callback(param);
};

var base = function() {
    val temp = null;        
    a(5, function(a){
        //can I do something like return 2*a and end the parent function
        temp = 2*a;
    });

    return temp;
}

I realised I actually needed to use sync version of mkpath as I dont want to run further code before that dir is created. so changed my code to

try {
    mkpath.sync(testFileDir);
} catch(err) {
    return res.status(500).send({error:'couldn\'t create directory to store log files'});
}
raju
  • 4,788
  • 15
  • 64
  • 119
  • Either a is synchronous, and then you don't need that, or it's asynchronous, and then it wouldn't work. We really need an example which would be less dumbed down. – Denys Séguret May 14 '14 at 15:41
  • I added actual code related to req,res. – raju May 14 '14 at 15:46
  • And the actual code changed the answer... – epascarello May 14 '14 at 15:47
  • It's the most asked question in JavaScript. Here's the most complete QA : http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – Denys Séguret May 14 '14 at 15:49
  • That is, the return value of asynchronous is not usually very useful, since the actual data is only available when the asynchronous callbacks are called. – Ruan Mendes May 14 '14 at 16:15

1 Answers1

1

On you dumbed down question (which is synchronous). Sure you could, but you'd have to return the value from your callback, your callback handler and your calling function.

var a = function (param, callback) {
  return callback(param);
};

var base = function () {
  return a(5, function (a) {
      return 2*a;
    });
}

This would not work in an asynchronous case, Then you need callback functions, or instead returning a deferred, promise or future.

For example

function asynchDoubler(number) {
  var deferred = new Deferred();
  setTimeout(function () {
      deferred.callback(2*number);
    }, 1);
  return deferred;
}

asynchDoubler(5).addCallback(function (result) { 
    console.log(result); 
  });
// outputs 10
Hampus
  • 2,769
  • 1
  • 22
  • 38
  • I realised I needed a sync version of mkpath as I dont want to execute further code before this dir gets created. mkpath.sync(testFileDir); path.exists(testFileDir, function(ex){ if (!err) res.send(500,{error:'couldn\'t create directory to store log files'}); }) – raju May 14 '14 at 16:16
  • As a general rule of thumb, you never want sync versions of anything async. The right way for you code to continue in the flow you want is to put it in a callback. Either by using helper objects such as deferreds or by using callback functions. – Hampus May 14 '14 at 16:45