0
0
Node.jsframework~5 mins

Error handling in async/await in Node.js - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the main way to handle errors when using async/await in Node.js?
Use a try/catch block around the await expression to catch and handle errors.
Click to reveal answer
beginner
Why should you avoid using .then() and .catch() with async/await?
Because async/await is designed to work with try/catch for clearer, simpler error handling and better readability.
Click to reveal answer
intermediate
What happens if an error is not caught inside an async function?
The async function returns a rejected Promise, which can cause unhandled promise rejection warnings if not handled elsewhere.
Click to reveal answer
intermediate
How can you handle multiple async operations with error handling?
Use try/catch around each await or use Promise.all with try/catch to handle errors from multiple async calls.
Click to reveal answer
beginner
Show a simple example of error handling using async/await in Node.js.
async function fetchData() {
  try {
    const response = await fetch('https://api.example.com/data');
    const json = await response.json();
    return json;
  } catch (error) {
    console.error('Error fetching data:', error);
  }
}
Click to reveal answer
What keyword is used to wait for a Promise to resolve inside an async function?
Aawait
Bwait
Casync
Dthen
How do you catch errors when using async/await?
AUsing console.log
BUsing try/catch blocks
CUsing setTimeout
DUsing if/else statements
What happens if you forget to use try/catch around an await that rejects?
AThe error is ignored silently
BThe program automatically retries
CThe program crashes immediately
DThe error becomes an unhandled promise rejection
Which of these is a good practice for handling multiple async calls with error handling?
AUse multiple nested callbacks
BIgnore errors for faster code
CWrap Promise.all inside try/catch
DUse synchronous loops
Can async functions return values directly?
ANo, they always return a Promise
BYes, like normal functions
COnly if you use return await
DOnly if you use callbacks
Explain how to handle errors in async/await functions in Node.js.
Think about how you catch errors in synchronous code and apply that to async functions.
You got /4 concepts.
    Describe what happens if an awaited Promise rejects and there is no try/catch block.
    Consider what happens when errors are not caught in normal code.
    You got /4 concepts.