0
0
Node.jsframework~20 mins

Error handling in async/await 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 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();
AA warning appears in the console about an unhandled promise rejection.
BThe program crashes immediately with an uncaught exception error.
CThe error is silently ignored and no output appears.
DThe function returns undefined without any error.
Attempts:
2 left
💡 Hint
Think about how Node.js handles promises that reject but are not caught.
📝 Syntax
intermediate
2: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?
A
try {
  const user = await getUser();
} catch (error) {
  console.error(error);
}
BgetUser().catch(error => console.error(error));
C
try {
  getUser().catch(error => console.error(error));
} catch (error) {
  console.error(error);
}
Dawait getUser().catch(error => console.error(error));
Attempts:
2 left
💡 Hint
Remember that await must be inside an async function and try/catch is used to catch errors.
🔧 Debug
advanced
2: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();
ABecause setTimeout does not allow errors to be caught by try/catch.
BBecause async functions cannot catch errors from setTimeout callbacks.
CBecause the error is thrown inside a callback, outside the try block's synchronous scope.
DBecause the catch block is missing an await keyword.
Attempts:
2 left
💡 Hint
Think about how try/catch works with asynchronous callbacks.
state_output
advanced
2: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();
ADone
BDone\nError: Oops
CError: Oops
DError: Oops\nDone
Attempts:
2 left
💡 Hint
Remember the order of try/catch/finally execution.
🧠 Conceptual
expert
2:00remaining
Which statement about async/await error handling is true?
Select the only true statement about error handling with async/await in Node.js.
AUnhandled promise rejections from async functions will always terminate the Node.js process immediately.
BUsing try/catch inside async functions allows synchronous-style error handling for asynchronous code.
CAwait expressions automatically log errors to the console if not caught.
DErrors thrown inside async functions must always be caught with try/catch; otherwise, the program crashes immediately.
Attempts:
2 left
💡 Hint
Think about how async/await simplifies error handling compared to raw promises.