Bird
Raised Fist0
Node.jsframework~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of using try/catch blocks with async/await in Node.js?
easy
A. To handle errors that occur during asynchronous operations
B. To make the code run faster
C. To avoid using promises
D. To automatically retry failed operations

Solution

  1. Step 1: Understand async/await behavior

    Async/await pauses code execution until a promise settles, but errors can still happen.
  2. Step 2: Role of try/catch

    Try/catch blocks catch errors thrown inside async functions to prevent crashes.
  3. Final Answer:

    To handle errors that occur during asynchronous operations -> Option A
  4. Quick Check:

    Error handling = D [OK]
Hint: Use try/catch to catch async errors safely [OK]
Common Mistakes:
  • Thinking try/catch makes code faster
  • Believing async/await removes need for error handling
  • Assuming errors auto-retry without code
2. Which of the following is the correct syntax to catch errors in an async function using async/await?
easy
A. async function fetchData() { await fetch(); } catch (e) { console.error(e); }
B. async function fetchData() { (await fetch()).catch(e => console.error(e)); }
C. async function fetchData() { try { await fetch(); } catch (e) { console.error(e); } }
D. async function fetchData() { try await fetch(); catch (e) { console.error(e); } }

Solution

  1. Step 1: Identify proper try/catch block usage

    Try/catch must wrap the await expression inside the async function body.
  2. Step 2: Check syntax correctness

    async function fetchData() { try { await fetch(); } catch (e) { console.error(e); } } correctly places try before await and catch after the block.
  3. Final Answer:

    async function fetchData() { try { await fetch(); } catch (e) { console.error(e); } } -> Option C
  4. Quick Check:

    Correct try/catch syntax = C [OK]
Hint: Wrap await inside try block, catch errors after [OK]
Common Mistakes:
  • Placing catch outside function body
  • Using .catch() on await directly
  • Using try without braces
3. What will be logged to the console when running this code?
async function test() {
  try {
    await Promise.reject('fail');
  } catch (error) {
    console.log('Caught:', error);
  }
}
test();
medium
A. fail
B. Caught: fail
C. Uncaught (in promise) fail
D. No output

Solution

  1. Step 1: Understand Promise.reject inside try block

    The rejected promise throws an error caught by the catch block.
  2. Step 2: Check console.log output

    The catch block logs 'Caught:' followed by the error message 'fail'.
  3. Final Answer:

    Caught: fail -> Option B
  4. Quick Check:

    Error caught and logged = B [OK]
Hint: Rejected promises inside try are caught by catch [OK]
Common Mistakes:
  • Expecting unhandled rejection error
  • Thinking error is logged without 'Caught:' prefix
  • Assuming no output because of rejection
4. Identify the error in this async function error handling code:
async function load() {
  try {
    const data = await fetchData();
  } catch {
    console.error(error);
  }
}
medium
A. console.error cannot log variables
B. Await cannot be used inside try block
C. fetchData() must be awaited outside try
D. Missing error parameter in catch block

Solution

  1. Step 1: Check catch block syntax

    The catch block is missing the error parameter to receive the thrown error.
  2. Step 2: Understand console.error usage

    console.error(error) references a variable 'error' which is undefined without catch parameter.
  3. Final Answer:

    Missing error parameter in catch block -> Option D
  4. Quick Check:

    Catch needs error param = A [OK]
Hint: Always name error in catch(e) to use it inside [OK]
Common Mistakes:
  • Omitting error parameter in catch
  • Misplacing await outside try
  • Misusing console.error syntax
5. You want to run multiple async tasks in parallel and handle errors individually without stopping all tasks. Which pattern correctly handles errors for each async call using async/await?
hard
A. Wrap each await call inside its own try/catch block
B. Use Promise.all with a single try/catch around it
C. Use await inside a forEach loop with one try/catch outside
D. Ignore errors and rely on process.on('unhandledRejection')

Solution

  1. Step 1: Understand parallel async calls and error handling

    Promise.all stops on first rejection; one try/catch around it catches all or none.
  2. Step 2: Individual error handling requires separate try/catch

    Wrapping each await in its own try/catch lets errors be handled separately without stopping others.
  3. Final Answer:

    Wrap each await call inside its own try/catch block -> Option A
  4. Quick Check:

    Individual error handling = A [OK]
Hint: Try/catch each await separately for independent error handling [OK]
Common Mistakes:
  • Using Promise.all with one try/catch stops all on first error
  • Using forEach with await causes unexpected behavior
  • Ignoring errors leads to crashes or silent failures