Answer the question
In order to leave comments, you need to log in
How to output the result of an async function to an external variable?
I am using the mysqljs library, which makes an asynchronous request to the database, and returns the result in a callback function:
var data = [];
connection.query('select * from `users` where `is_enabled` = 1;', (err, response) => {
if(err) throw err;
data = response.map(item => item.value);
console.log(data)//array data
});
console.log(data)//undefined
Answer the question
In order to leave comments, you need to log in
This is called callback hell, they did it before, but now you can use async await
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question