0
0
Node.jsframework~20 mins

Async/await error handling patterns in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async/Await Error Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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();
AUncaught (in promise) Error: Failed
BData: undefined
CCaught: Failed
DNo output
Attempts:
2 left
💡 Hint
Think about what happens when a promise rejects inside a try block with await.
📝 Syntax
intermediate
2: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?
Atry { await getUser(); } catch (error) { console.log('Error:', error.message); }
Btry { getUser(); } catch (error) { console.log('Error:', error.message); }
Cawait getUser().catch(error => console.log('Error:', error.message));
DgetUser().catch(error => console.log('Error:', error.message));
Attempts:
2 left
💡 Hint
Remember that await must be inside an async function to use try/catch.
🔧 Debug
advanced
2: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();
ABecause try/catch cannot catch errors from Promise.reject at all.
BBecause Promise.reject is not awaited, so error is unhandled outside try/catch.
CBecause the error message is empty, so nothing is logged.
DBecause load() is not awaited, so errors are ignored.
Attempts:
2 left
💡 Hint
Think about how async errors propagate when not awaited.
state_output
advanced
2: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();
AInner catch: Fail
BOuter catch: Fail
CNo output
D
Inner catch: Fail
Outer catch: New error
Attempts:
2 left
💡 Hint
Look at how errors are re-thrown and caught by outer catch.
🧠 Conceptual
expert
2: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.
AWrap all awaits in a single try/catch block to catch any error thrown.
BUse multiple try/catch blocks around each await call separately.
CUse .then().catch() chaining for each async call instead of async/await.
DIgnore errors and rely on process.on('unhandledRejection') to catch them.
Attempts:
2 left
💡 Hint
Think about clean and centralized error handling with async/await.