I have the following;
(我有以下内容;)
function isUserInDatabase(serverID, playerID) {
return new Promise((resolve, reject) => {
executeQuery("SELECT * FROM playerdata where serverID=" + serverID + " AND playerID=" + playerID).then((res) => {
if (res[0] === undefined) {
resolve(false);
} else {
resolve(true);
}
});
}).catch ((e) => {
console.error(e);
console.log("Error retrieving data from database.");
});
}
But I have no reject call.
(但是我没有拒绝电话。)
Is this bad convention?(这是不好的约定吗?)
Edit: I'm honestly not sure if this is better.
(编辑:老实说,我不确定这是否更好。)
I've done a bit more reading into promises, and perhaps this is a little better, but I'm not sure.(我已经读了一些关于诺言的文章,也许这会更好一些,但是我不确定。)
async function handlePlayer(serverID, playerID) { //TEST
console.log(await isUserInDatabase(serverID, playerID));
}
function isUserInDatabase(serverID, playerID) {
return executeQuery("SELECT * FROM playerdata where serverID=? AND playerID=?", [serverID, playerID]).then((res) => {
if (res[0] === undefined) {
return false;
}
return true;
})
.catch ((err) => {
console.log(err);
});
}
async function executeQuery(query, opts) {
let conn;
try {
conn = await pool.getConnection();
return await conn.query(query, opts);
} catch (err) {
console.log(err);
} finally {
conn.end();
}
}
ask by Kwxhvor translate from so