I'm trying to assign a function's return value to a variable, using this stackOverflow article as a template. Here's my code:
var host = getHostName(djId, function(name) {
return name;
});
console.log(host); // undefined :(
function getHostName(id, cb) {
var reply;
userColl.findById(id, function (err, dj) {
if (err) {
reply = "db error";
} else {
reply = dj.firstName + ' ' + dj.lastName;
}
console.log(reply + ' reply'); // working as expected!
cb(reply);
});
}
the console.log(reply) is working as expected but host
is still undefined when i try t call the function. what gives?