If you want to use async functions with Express, there’s a simple hack for that:
wrap your async
function in this expressify
utility function.
function expressify(fn) {
return (req, res, next) => {
fn(req, res, next).catch(next)
}
}
Usage:
import { setTimeout } from 'timers/promises'
async function myAsyncHelloWord(req, res, next) {
await setTimeout(1337);
res.send('Hello World!')
}
app.get('/', expressify(myAsyncHelloWord))