Challenge - 5 Problems
Async/Await Error Handling 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 function with try/catch?
Consider this Node.js async function. What will it print to the console when called?
Node.js
async function fetchData() { try { const data = await Promise.reject(new Error('Failed')); console.log('Data:', data); } catch (error) { console.log('Caught:', error.message); } } fetchData();
Attempts:
2 left
💡 Hint
Think about what happens when a promise rejects inside a try block with await.
✗ Incorrect
The rejected promise throws an error caught by the catch block, so it logs 'Caught: Failed'.
📝 Syntax
intermediate2:00remaining
Which option correctly handles errors with async/await?
Which code snippet correctly catches errors from an async function call?
Node.js
async function getUser() { throw new Error('User not found'); } // Which option below correctly handles errors?
Attempts:
2 left
💡 Hint
Remember that await must be inside an async function to use try/catch.
✗ Incorrect
Only option A uses await inside try/catch correctly to catch async errors.
🔧 Debug
advanced2:00remaining
Why does this async function not catch the error?
Identify why the error is not caught in this code snippet.
Node.js
async function load() { try { Promise.reject(new Error('Oops')); } catch (e) { console.log('Caught:', e.message); } } load();
Attempts:
2 left
💡 Hint
Think about how async errors propagate when not awaited.
✗ Incorrect
Without await, the rejected promise runs asynchronously and is not caught by try/catch.
❓ state_output
advanced2:00remaining
What is the console output of this async/await with nested try/catch?
Analyze the output of this code snippet:
Node.js
async function test() { try { try { await Promise.reject('Fail'); } catch (e) { console.log('Inner catch:', e); throw new Error('New error'); } } catch (e) { console.log('Outer catch:', e.message); } } test();
Attempts:
2 left
💡 Hint
Look at how errors are re-thrown and caught by outer catch.
✗ Incorrect
The inner catch logs 'Fail' then throws a new error caught by outer catch logging 'New error'.
🧠 Conceptual
expert2:00remaining
Which pattern best ensures all async errors are caught in a Node.js app?
Choose the best pattern to handle errors from multiple async calls in a function.
Attempts:
2 left
💡 Hint
Think about clean and centralized error handling with async/await.
✗ Incorrect
A single try/catch around multiple awaits cleanly catches any error thrown by them.