This is throwing me for a bit of a loop; I’m not entirely sure how to phase the question. I have an object with several properties which are fetched from, and written to, a database. An example might be:
// This is an over simplified example. this._connection() returns a valid DB connection
var delay = {
'time' : function() {
this._connection().query('SELECT * FROM delay', function(err, result, fields) {
return result.length ? result[0].time : 60;
}
}
}
Now, if I try to use this code ….
console.log('Current Delay:' + delay.time());
It prints, as I should have expected, ‘undefined’ because the mySQL callback didn’t execute before the method delay() returned, and so the return inside the callback doesn’t do anything.
Is there an accepted method for dealing with this sort of thing? Maybe I’m just unable to wrap my head around node.js/async.
It’s called callbacks
Note that
returnstatements in inner functions are useless.returndoes not magically propogate to callreturnon the outer function. That’s because functions have an implecitreturn undefined;at the end of them