Node.js & Libuv•Part 4: Express, Fastify, APIs & Middleware (36 - 50)
`next(err)` vs throwing asynchronous errors in Express.
In Simple Words:
In Express 4, throwing an error inside an asynchronous Promise handler unhandled will crash the process or hang the request unless passed via next(err). (Express 5 natively catches rejected promises from async handlers).
javascript
// Express 4 async wrapper pattern:
const asyncHandler = (fn) => (req, res, next) => {
Promise.resolve(fn(req, res, next)).catch(next);
};
app.get('/users', asyncHandler(async (req, res) => {
const users = await db.getUsers();
res.json(users);
}));