In my express.js app, I would like to encapsulate my async database call into reusable function "getPerson(id)". Like so...
function getPerson(id) {
db.get(
`SELECT * FROM People WHERE id = ${id};`,
(error, row) => {
if (error || !row) {
return false
} else {
return row.name
}
}
)
}
But how can I "await" result from this function inside express.js request route block of code? Like so...
app.get('/person/:id', (req, res) => {
const person = getPerson(req.params.id) // wait for this, but how?
if (person) {
res.status(200).send(person)
} else {
res.sendStatus(400)
}
})
I have tried different combination of using async/await keywords in above code samples, but not sure how that works. Then I also read in express.js docs that async/await keywords are not even supported in express version <5. Above node.js examples use express.js 4.x and sqlite3 5.x modules. Thanks for the help.