nếu bạn tình cờ ở trong Node 8+ , bạn có thể tận dụng util.promisify()
gốc với nút mysql.
Đừng quên gọi nó bằng bind()
vì vậy this
sẽ không lộn xộn:
const mysql = require('mysql'); // or use import if you use TS
const util = require('util');
const conn = mysql.createConnection({yourHOST/USER/PW/DB});
// node native promisify
const query = util.promisify(conn.query).bind(conn);
(async () => {
try {
const rows = await query('select count(*) as count from file_managed');
console.log(rows);
} finally {
conn.end();
}
})()