I wrote the following code, where I have a post request that executes 2 queries and some other operations:
router.post('/', (req, res) => {
dbController.query(
"SELECT * FROM users WHERE username = 'myUserName'",
(err, result) => {
console.log('<---- 1 ---->')
}
)
// do something
console.log('<---- 2 ---->')
// do something
dbController.query(
"SELECT * FROM users WHERE username = 'myUserName'",
(err, result) => {
console.log('<---- 3 ---->')
})
res.send('ok')
})
I want to execute all instructions inside the function sequentially, so instead of getting this output (which is the one I get after executing the code):
<---- 2 ---->
<---- 1 ---->
<---- 3 ---->
I wanna get this one:
<---- 1 ---->
<---- 2 ---->
<---- 3 ---->
Note that after adding the keyword async
to the function and await
to the queries, nothing changes