@dedrick
To return a result value from an async function in Node.js with MySQL, you can follow these steps:
1 2 3 4 5 6 7 8 9 10 |
const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password', database: 'my_database' }); connection.connect(); |
1 2 3 4 5 6 7 8 9 10 11 |
async function getData() { return new Promise((resolve, reject) => { connection.query('SELECT * FROM my_table', (error, results) => { if (error) { reject(error); } else { resolve(results); } }); }); } |
1 2 3 4 5 6 7 8 |
getData() .then((result) => { console.log(result); // Do something with the result value }) .catch((error) => { console.error(error); }); |
By following these steps, you can return a result value from an async function in Node.js with MySQL.