When working with the Testing function, it is important to remember that it operates asynchronously, resulting in the return of a Promise.
To access the value of a Promise, you must utilize either the await
syntax or the .then() method.
Additionally, in order to obtain the output, the query result needs to be passed to the res callback:
// For demonstration purposes, we create a mock db object...
const db = {
query(sql, cb) {
console.log('Executing query:', sql);
setTimeout(() => cb(null, [{ id: 1, data: 'some data' }]), 500)
}
}
async function Testing() {
return new Promise((res, rejects) => {
db.query("SELECT * FROM `nitrоboost`", (error, row) => {
if (error) {
console.log(error);
// Error passed to the rejects callback
rejects(error);
} else {
// Result passed to the res callback
res(row)
}
});
});
}
async function runTesting() {
let message = await Testing();
console.log(`result:`, message);
}
runTesting();
.as-console-wrapper { max-height: 100% !important; top: 0; }