I have an issue while I go through this function. It doesn't render values in the good order. I tried to use async/await, but maybe I'm not using it in proper way. Here db is for mySQL.
function getUserInfo() {
db.connect(function (err) {
if (err) throw err;
db.query("SELECT id FROM game WHERE id = '" + authorId + "'", function (err, result, field) {
if (result.length === 0) {
var sql = "INSERT INTO game (id, user, coins) VALUES (" + authorId + ", '" + authorName + "', 0)";
db.query(sql, function (err, result) {
if (err) throw err;
console.log("Données enregistré");
result = 0
return new Promise((resolve) => {
if (true) {
resolve(result);
}
})
});
} else {
var sql = "SELECT coins FROM game WHERE id = " + authorId + "";
db.query(sql, function (err, result) {
if (err) throw err;
console.log(result[0].coins);
return new Promise((resolve) => {
if (true) {
resolve(result[0].coins);
}
})
});
}
})
});
}
async function operationAsync() {
const result = await getUserInfo()
console.log(result)
}
operationAsync()
I have this result:
undefined
Données enregistré
I want the first function to be done before trying to display his result.
Any idea ?