0
0
Expressframework~20 mins

Async middleware wrapper in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Middleware Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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');
}));
AThe server crashes with an unhandled promise rejection error.
BThe request hangs indefinitely with no response.
CThe client receives a 200 OK response with an empty body.
DThe error is passed to Express error handling middleware via next(), resulting in a 500 response.
Attempts:
2 left
💡 Hint
Think about how Promise.catch(next) works in Express middleware.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines an async middleware wrapper in Express?
Select the option that correctly implements an async middleware wrapper function for Express.
Aconst asyncWrapper = fn => async (req, res, next) => { await fn(req, res, next).catch(next); };
Bconst asyncWrapper = fn => (req, res, next) => { Promise.resolve(fn(req, res, next)).catch(next); };
Cconst asyncWrapper = fn => (req, res, next) => { try { fn(req, res, next); } catch (e) { next(e); } };
Dconst asyncWrapper = fn => (req, res, next) => { fn(req, res, next).catch(next); };
Attempts:
2 left
💡 Hint
Remember that async functions return promises and errors must be caught asynchronously.
🔧 Debug
advanced
2: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);
  }
};
AThe try/catch block does not catch errors from the async function because it returns a promise and errors happen asynchronously.
BThe next function is not called correctly inside the catch block.
CThe wrapper should use setTimeout to catch errors asynchronously.
DThe async function is not awaited, causing the wrapper to miss synchronous errors.
Attempts:
2 left
💡 Hint
Think about how errors in promises behave with try/catch.
state_output
advanced
2: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 true
Atrue
Bfalse
Cundefined
Dnull
Attempts:
2 left
💡 Hint
The catch block sets errorHandled to true when an error occurs.
🧠 Conceptual
expert
3: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.
AAsync middleware wrappers improve performance by running middleware functions in parallel.
BWrapping async middleware automatically converts them into synchronous functions, simplifying error handling.
CExpress does not natively handle rejected promises in async middleware, so errors must be caught and passed to next() to avoid unhandled promise rejections and server crashes.
DAsync middleware wrappers are only needed when using Express with TypeScript.
Attempts:
2 left
💡 Hint
Consider how Express handles errors from promises by default.