1
\$\begingroup\$

Is this the right way to use async/await with the JavaScript MySQL client? I had to use the promise-mysql package because it wraps the function calls with Bluebird promises, so that I'm able to use async/await.

const express = require('express');
const mysql = require('promise-mysql');

let app = express();

let pool = mysql.createPool({
    host: '',
    user: '',
    password: '',
    database: ''
});

app.get('/', async (req, res) => {
    try {
        let conn = await pool.getConnection();
        let results = await conn.query('SELECT 1 + 1');
        res.send(results[0]);
    } catch (err) {
        return res.send(err);
    }
});

app.listen(3000, () => {
    console.log('server is listening on port 3000');
});
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.