Answer the question
In order to leave comments, you need to log in
How to fetch result from object's async function using async/await?
var sessions = [];
async function setSessionsList() {
return new Promise((resolve,reject) => {
connection.query('select `device_ip` from `setup_devices` where `is_enabled` = 1;', (err, data) => {
if(err) reject(err);
data.forEach(item =>
sessions.push(new snmp.createSession(item.device_ip,community,options)));
return resolve(sessions);
});
});
}
setSessionsList().then( resolve => { /*Do somth... */} ).catch( reject => console.error( reject ));
function foo() {
let data = setSessionsList();
console.log(data);//Query result...
}
Answer the question
In order to leave comments, you need to log in
The function is described correctly, it became necessary to add await and make sure that it is in the async scope
async function foo(){
let sessionsList = await setSessionsList(); //логичнее правда назвать getSessionsList
console.log(sessionsList); // дождется результата и только потом сработает.
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question