Performance: Async middleware wrapper
MEDIUM IMPACT
This concept affects server response time and error handling efficiency, impacting how quickly the server can respond to requests without blocking.
const asyncWrapper = fn => (req, res, next) => {
Promise.resolve(fn(req, res, next)).catch(next);
};
app.use(asyncWrapper(async (req, res, next) => {
const data = await someAsyncFunction();
res.send(data);
}));app.use(async (req, res, next) => { const data = await someAsyncFunction(); res.send(data); // No try/catch or error forwarding });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Async middleware without wrapper | N/A | N/A | N/A | [X] Bad |
| Async middleware with wrapper | N/A | N/A | N/A | [OK] Good |