In the following code, I am trying to put the results from the first and second query inside a global variable called result
. The problem is Promise.all()
is not waiting the queries finish and before proceding to then()
.
How can I solve it?
Code:
var result = {};
Promise.all([
connection.query('SELECT * FROM analysis',
function(err, rows, fields) {
if (err) throw err;
result.analysis = rows;
console.log("query 1");
}),
connection.query('SELECT * FROM analysis_description',
function(err, rows, fields) {
if (err) throw err;
result.analysis_description = rows;
console.log("query 2");
})
])
.then(function(result){
console.log(result);
console.log("result");
});
Output:
result
query 1
query 2