1
I have an asynchronous function that I need to use await
in the return of the database to use this data later when doing the next insertion using the LAST_USER_ID()
MYSQL, however, using the await
he to the execution there and apparently stands expecting more results than he already has and does not proceed further.
When I don’t use await
and put a setInterval()
to call a function and make the next insertion everything happens as expected, otherwise it tries to execute before having the result of the previous.
Apparently the await
expects more results than I’m expecting and never skips to the next part.
Could someone help me find where I’m going wrong?
Follow the code below:
function sqlquery(str, params) {
return new Promise((resolve, reject) => {
con.query(str, params, (err, result) => {
if (err) reject(err);
resolve(result);
})
})
}
async function tudo() {
console.log(`\n> Registrando 1...\n`)
await sqlquery("INSERT INTO `log` (`data`, `horainicio`) VALUES (CURRENT_DATE(), CURRENT_TIME());", function lastId(err, result) {
if (err) throw err;
idRegistro = result.insertId
console.log(`\n> Registrado 1.\n`);
});
console.log("\n> Registrando 2...\n");
await sqlquery(`UPDATE log SET horafim = CURRENT_TIME() WHERE idlog = ${idRegistro}`)
console.log("\n> Registrado 2.\n");
}
tudo();
outworking
node mysql.js
> Registrando 1...
> Registrado 1.