Challenge - 5 Problems
Async Middleware Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this async middleware wrapper?
Consider this Express async middleware wrapper function. What will be the response if the wrapped async function throws an error?
Express
const asyncWrapper = fn => (req, res, next) => {
Promise.resolve(fn(req, res, next)).catch(next);
};
app.get('/test', asyncWrapper(async (req, res) => {
throw new Error('Oops');
}));Attempts:
2 left
💡 Hint
Think about how Promise.catch(next) works in Express middleware.
✗ Incorrect
The asyncWrapper catches any error thrown in the async function and passes it to next(), which triggers Express's error handling middleware. This prevents the server from crashing and sends a 500 error response.
📝 Syntax
intermediate2:00remaining
Which option correctly defines an async middleware wrapper in Express?
Select the option that correctly implements an async middleware wrapper function for Express.
Attempts:
2 left
💡 Hint
Remember that async functions return promises and errors must be caught asynchronously.
✗ Incorrect
Option B correctly wraps the async function call in Promise.resolve and catches errors to pass to next(). Option B misses Promise.resolve and may not catch non-promise errors. Option B misuses await with catch. Option B uses try/catch but does not handle async errors properly.
🔧 Debug
advanced2:00remaining
Why does this async middleware wrapper fail to catch errors?
Identify the problem in this async middleware wrapper code that causes errors thrown in the async function not to be caught.
Express
const asyncWrapper = fn => (req, res, next) => {
try {
fn(req, res, next);
} catch (err) {
next(err);
}
};Attempts:
2 left
💡 Hint
Think about how errors in promises behave with try/catch.
✗ Incorrect
Errors thrown inside async functions are wrapped in promises and happen asynchronously. The try/catch block only catches synchronous errors, so it misses async errors. The wrapper must catch promise rejections instead.
❓ state_output
advanced2:00remaining
What is the value of 'errorHandled' after this middleware runs?
Given this async middleware wrapper and route, what will be the value of the variable 'errorHandled' after a request triggers an error?
Express
let errorHandled = false;
const asyncWrapper = fn => (req, res, next) => {
Promise.resolve(fn(req, res, next)).catch(err => {
errorHandled = true;
next(err);
});
};
app.get('/fail', asyncWrapper(async (req, res) => {
throw new Error('Fail');
}));
// Simulate a request to '/fail' and error handling middleware sets errorHandled to trueAttempts:
2 left
💡 Hint
The catch block sets errorHandled to true when an error occurs.
✗ Incorrect
When the async function throws, the catch block runs, setting errorHandled to true before calling next with the error.
🧠 Conceptual
expert3:00remaining
Which statement best explains why async middleware wrappers are important in Express?
Choose the best explanation for why wrapping async middleware functions with an error-catching wrapper is necessary in Express applications.
Attempts:
2 left
💡 Hint
Consider how Express handles errors from promises by default.
✗ Incorrect
Express does not catch rejected promises from async middleware automatically. Without a wrapper, unhandled promise rejections can crash the server or cause silent failures. Wrapping async middleware ensures errors are forwarded to Express error handlers.