This question already has an answer here:
Javascript async and await is confusing the heck out of me.
I am just trying to a simple DB query and return the result. Here I am passing in userId
as a reference to an object to modify. and once I query the the statement, I want to add result into userId and print it out.
db.connect();
async function main(userIds) {
try {
const query = util.promisify(db.query).bind(db);
await query("select distinct ownerId from ListingTbl")
.then(result => {
const rows = JSON.parse(JSON.stringify(result));
for (let i = 0; i < rows.length; i++) {
const r = JSON.parse(JSON.stringify(rows[i]));
userIds.add(r.ownerId);
console.log("done")
}
})
.catch(e => {
throw e;
});
} catch (e) {
throw e;
}
}
var userIds = new Set();
main(userIds);
console.log("OK");
console.log(userIds);
db.end();
But here I am getting empty userId? I understand await
does not block main thread. But then how do I block the main thread from execution?
OK
Set {}
done