I am new in nodejs, while I write a function, it's not working for return a value in function. I fetch result from mysql then return to function. I know the reason because nodejs default is async. How can I fix my problem? this my source code:
function getSlaveURL() {
var sqlQuery = "SELECT url FROM `slave` WHERE `id`='2' LIMIT 1";
var returnValue = "";
db.query(sqlQuery, function (error, rows) {
if (error) {
returnValue = "";
} else {
returnValue = rows[0]['url'];
console.log("in function result: " + returnValue);
}
});
return returnValue;
}
console.log("out function result: " + getSlaveURL());
=> result:
out function result:
in function result: http://localhost/mynodejs_slave/
Please help me to fix this problem. Thank you!