0
0
Node.jsframework~5 mins

Promise catch for async errors in Node.js - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the catch method do in a Promise?
The catch method handles errors or rejections that happen in the Promise chain. It lets you respond to problems without crashing your program.
Click to reveal answer
beginner
How do you attach a catch to an async function call that returns a Promise?
You call the async function and then add .catch(error => { /* handle error */ }) after it to catch any errors that happen inside the async function.
Click to reveal answer
beginner
Why is it important to use catch with Promises?
Without catch, errors inside Promises can go unnoticed and cause your app to behave unexpectedly or crash. catch helps keep your app stable by handling those errors.
Click to reveal answer
intermediate
What happens if you forget to add catch to a Promise?
If you forget catch, unhandled Promise rejections may occur, which can cause warnings or crashes depending on your Node.js version and settings.
Click to reveal answer
beginner
Show a simple example of using catch with an async function.
Example:<br><pre>async function fetchData() {
  throw new Error('Oops!');
}

fetchData()
  .catch(error =&gt; console.log('Error caught:', error.message));</pre>
Click to reveal answer
What does promise.catch() do?
AHandles errors from the Promise
BStarts a new Promise
CResolves the Promise successfully
DCancels the Promise
Which of these is the correct way to catch errors from an async function getData()?
AgetData().catch(error =&gt; console.log(error))
Bcatch(getData())
CgetData().then(error =&gt; console.log(error))
DgetData().error()
What happens if you don't use catch on a Promise that rejects?
AThe Promise retries automatically
BThe error is ignored silently
CThe Promise resolves anyway
DNode.js may show an unhandled rejection warning or crash
Can catch be used to handle errors from multiple Promises chained together?
ANo, each Promise needs its own catch
BYes, one catch at the end can handle errors from the whole chain
COnly if Promises are run in parallel
DOnly if Promises are synchronous
Which Node.js version introduced warnings for unhandled Promise rejections by default?
ANode.js 6
BNode.js 8
CNode.js 10
DNode.js 12
Explain how to use catch to handle errors from an async function returning a Promise.
Think about how you would stop your app from crashing when an async call fails.
You got /4 concepts.
    Why is it important to always add a catch when working with Promises in Node.js?
    Consider what happens if errors are ignored in your code.
    You got /4 concepts.