The sql.query function uses a callback meaning it is asynchronous code. You cannot resolve the result synchronously. I am not sure whether you are using the sql or mssql npm library, but I'd suggest looking if there is a native promise approach or to wrap your code into a promise oriented approach. If you're using the mssql library, when omitting the callback, it returns automatically a promise and you can use it like this.
function sqllis(sql_q) {
sql.connect(dbConfig, function (err) {
if (err) console.log(err);
var request = new sql.Request();
return request.query(sql_q);
});
}
and use it asynchronously like this:
sqllis("select * from dbo.sys_user ")
.then(function(results){ console.log(results) })
.catch(function(err){ console.log(err) });
You can also pass a callback function.
function sqllis(sql_q, callback) {
sql.connect(dbConfig, function (err) {
if (err) console.log(err);
var request = new sql.Request();
request.query(sql_q, callback);
});
}
and use it like this
sqllis("select * from dbo.sys_user ", function(err, results){
console.log(err);
console.log(results); //results.recordsets
})