Async/await try-catch blocks and unhandled promise rejections
0 reputation · 16 Jan 2021, 04:25 UTC
When implementing asynchronous recovery logic in JavaScript, the async/await syntax is typically paired with try...catch...finally blocks to manage failed operations. A critical requirement is ensuring that every asynchronous failure is captured to prevent process termination in environments like Node.js, where unhandled promise rejections are treated as fatal errors.
There is a design tension between handling errors locally within the specific async function to perform immediate recovery and propagating those errors to a global handler for centralized logging. If a promise-returning function is called without the await keyword inside a try block, the rejection bypasses the local catch block entirely.
- Does propagating errors to a global handler interfere with the ability of a
finallyblock to execute local cleanup? - What is the most reliable pattern to ensure a rejection is captured when mixing
async/awaitwith legacy.then().catch()chains?