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 happens when an async function throws an error without try/catch?
Consider this async function in Node.js. What will be the output when calling
fetchData() without a try/catch block?Node.js
async function fetchData() { throw new Error('Failed to fetch'); } fetchData();
Attempts:
2 left
💡 Hint
Think about how Node.js handles promises that reject but are not caught.
✗ Incorrect
When an async function throws an error and the returned promise is not handled with catch, Node.js emits a warning about an unhandled promise rejection. It does not crash immediately but warns the developer.
📝 Syntax
intermediate2:00remaining
Which option correctly catches errors in async/await?
You want to catch errors thrown inside an async function. Which code snippet correctly handles errors?
Node.js
async function getUser() { // fetch user data } // How to call getUser with error handling?
Attempts:
2 left
💡 Hint
Remember that await must be inside an async function and try/catch is used to catch errors.
✗ Incorrect
Option A uses try/catch with await inside an async function, which is the standard way to catch errors synchronously. Option A works but does not use await. Option A is redundant and incorrect syntax. Option A is invalid outside async functions.
🔧 Debug
advanced2:00remaining
Why does this async function not catch the error as expected?
Look at the code below. Why does the error not get caught by the catch block?
Node.js
async function process() { try { setTimeout(() => { throw new Error('Timeout error'); }, 100); } catch (e) { console.log('Caught:', e.message); } } process();
Attempts:
2 left
💡 Hint
Think about how try/catch works with asynchronous callbacks.
✗ Incorrect
The error is thrown asynchronously inside the setTimeout callback, which runs after the try block has finished. So the catch block does not catch it. Errors in async callbacks need separate handling.
❓ state_output
advanced2:00remaining
What is the output of this async/await error handling code?
Analyze the code and select the console output.
Node.js
async function test() { try { await Promise.reject('Oops'); } catch (e) { console.log('Error:', e); } finally { console.log('Done'); } } test();
Attempts:
2 left
💡 Hint
Remember the order of try/catch/finally execution.
✗ Incorrect
The catch block logs the error first, then the finally block runs and logs 'Done'. So the output is 'Error: Oops' followed by 'Done'.
🧠 Conceptual
expert2:00remaining
Which statement about async/await error handling is true?
Select the only true statement about error handling with async/await in Node.js.
Attempts:
2 left
💡 Hint
Think about how async/await simplifies error handling compared to raw promises.
✗ Incorrect
Try/catch inside async functions lets you handle errors like synchronous code, making async code easier to read and maintain. The other statements are false regarding Node.js behavior.