I'm trying to use sql queries as promises. I can't seem to get it to work:
query: (sql, args) => {
if (args) sql = mysql.format(sql, args);
return new Promise((resolve, reject) => {
pool.getConnection((err, connection) => {
if (err) {
console.log(err);
reject(Error(err.code));
}
connection.query(sql, (err, results) => {
connection.release(); // always put connection back in pool after last query
if (err) {
console.log(err);
resolve([]);
}
resolve(results);
});
});
});
},
And here is the query itself:
async function dbCall(sql, arg) {
let data = await db.query(sql, arg);
console.log(data);
data = data[0];
return data;
}
And here is the pool:
var pool = mysql.createPool({
host: 'localhost',
user: 'user',
password: 'pass',
database: 'db',
});
What I'm trying to do: I'm trying to have it where it doesn't get hung up on async functions. I want it to return a value throughout a whole async function instead of inside of itself only.
Right now, it isn't working at all. However, when it is I would like row
to be defined in my whole function instead of just inside the db.query
.
I'm not sure if this makes sense, but if you need more clarification just ask anything.