0
0
Expressframework~10 mins

Async middleware wrapper in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create an async middleware wrapper function.

Express
const asyncWrapper = (fn) => (req, res, next) => {
  Promise.resolve(fn(req, res, next)).[1](next);
};
Drag options to blanks, or click blank then click option'
Aresolve
Bthen
Cfinally
Dcatch
Attempts:
3 left
💡 Hint
Common Mistakes
Using then instead of catch causes errors to be unhandled.
Using finally does not pass errors to next.
Using resolve is incorrect as it's not a Promise method.
2fill in blank
medium

Complete the code to use the async middleware wrapper with an async route handler.

Express
app.get('/data', [1](async (req, res) => {
  const data = await fetchData();
  res.json(data);
}));
Drag options to blanks, or click blank then click option'
AasyncHandler
BexpressAsyncHandler
CasyncWrapper
DwrapAsync
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined wrapper function names.
Not wrapping the async function causes unhandled errors.
3fill in blank
hard

Fix the error in the async middleware wrapper to correctly handle errors.

Express
const asyncWrapper = (fn) => (req, res, next) => {
  fn(req, res, next).[1](next);
};
Drag options to blanks, or click blank then click option'
Acatch
Bthen
Cfinally
Dresolve
Attempts:
3 left
💡 Hint
Common Mistakes
Using then instead of catch.
Not handling errors causes unhandled Promise rejections.
4fill in blank
hard

Fill both blanks to create an async middleware wrapper that returns a function with correct parameters.

Express
const asyncWrapper = ([1]) => ([2]) => {
  return Promise.resolve([1](...[2])).catch(next);
};
Drag options to blanks, or click blank then click option'
Afn
Breq, res, next
C[req, res, next]
Dargs
Attempts:
3 left
💡 Hint
Common Mistakes
Using parameter names instead of an array for spreading.
Not spreading the arguments causes errors.
5fill in blank
hard

Fill all three blanks to create a reusable async middleware wrapper with error forwarding.

Express
function asyncWrapper([1]) {
  return function [2](req, res, next) {
    [3](fn(req, res, next)).catch(next);
  };
}
Drag options to blanks, or click blank then click option'
Afn
Bmiddleware
CPromise.resolve
DasyncWrapper
Attempts:
3 left
💡 Hint
Common Mistakes
Not wrapping the function call in Promise.resolve.
Using incorrect function names.
Not passing errors to next.