примерно так. Вроде бы нода последних версий умеет async/await
var sql = "select * from table";
function getData(query) {
return new Promise(function(resolve, reject) {
var mssql = require('mssql');
var config = {
...
}
}
var connection = new mssql.connect(config, function(err) {
if (typeof err !== "undefined" && err !== null) {
reject(err);
return
}
var request = new mssql.Request(connection);
request.query(query, function(err, recordset) {
if (err != null) {
reject(err);
} else {
var data = {field1: [],field2: []};
for(var i = 0; i < recordset.recordset.length; i++){
data.field1.push(recordset.recordset[i].field1);
data.field2.push(recordset.recordset[i].field2);
}
resolve(data);
}
mssql.close();
});
});
});
}
async function main() {
var getArr = await getData(sql);
console.log(getArr.field1[0]);
console.log(getArr.field2[0]);
}
main();