← Back
express typescript

async handlers in express

I've recently realized that you don't need promises support (missing in express 4.x) to use async/await.

Just write a normal handler as an async function and wrap everything in a try catch to handle errors:

app.get("/users", async (req, res, next) => {
try {
await authorizeUser(req, res);
const users = await db.findAllUsers();
res.json(users);
} catch (err) {
next(err);
}
});