How do you implement error handling in Express.js applications?
Instruction: Compare Express 4 and 5 asynchronous error handling. Provide complete error middleware and demonstrate synchronous throws, rejected promises, and a timer or callback boundary.
Context: This question tests the candidate's ability to effectively manage errors in Express.js, ensuring robust application behavior.
Updated
Official answer available
Read the opening below, then unlock the full answer and practical guidance.
Put four-argument error middleware after routes. Express catches synchronous route throws. Express 5 also forwards rejections from returned promises; Express 4 needs explicit forwarding, such as a user-defined promise wrapper. Express has no built-in wrap helper.
This CommonJS example works with Express 4 or 5...