In Node.JS, I have a function to connect with a MySQL database, get a bunch of rows, and then return them as an array. My problem is with asynchronous
code, I am using the async
Node.JS module.
This is my function so far:
var async = require("async")
function get_all_channels_by_order(){
async.waterfall([
function(callback){
mysql_connection.connect()
mysql_connection.query("SELECT * FROM channels ORDER BY listorder ASC", function(err, result){
callback(null, result)
})
}
], function(err, result){
mysql_connection.end()
return result
})
//return ?? somehow, return the value from the MySQL query?
}
console.log(get_all_channels_by_order())
The problem appears when the line to call for the function already checks for the return value even though it wasn't sent yet.
How can this be done? Is it even possible? I realize I can just not use this function and manually do the query when ever I need it but that would end up completely not organized and messy.
I previously asked this question 'Change a variable from outside it's scope' which was marked as a duplicate of 'Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference', that question talks about asynchronous code, not how to have a function return a value from it and I can not figure out how.