-
-
Notifications
You must be signed in to change notification settings - Fork 657
Closed
Labels
Description
Its forcing me to put my entire node program in async function main() {} just because for some reason const connection = await mysql.createConnection must have await in it, in order to use async in queries.
This seems extremely unnecessary, just because i want to use await in queries that might not run directly after, i shouldnt need to await connection.
Please dont require the use of async for the connection in order for async in queries to work, just like with normal promises, there is no difference between them other than syntax sugar.
Here is a code example
async function main() {
const exp = require("express")
const cors = require("cors")
const mysql = require('mysql2/promise");
const app = exp()
const connection = await mysql.createConnection({
host: 'localhost',
user: 'root',
password: "",
database: 'my_database'
})
app.use(exp.json())
app.use(cors())
app.get("/test", async (req, res) => {
try {
const [ rows ]= await connection.execute(`SELECT * FROM users WHERE username = "e"`)
res.send({ data: rows })
} catch (error) {
console.log(error)
res.status(500).send({ err: "fail" })
}
})
app.listen(1000, () => console.log("localhost:1000"))
}
main()