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
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 => 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
✗ Incorrect
catch is used to handle errors or rejections from a Promise.
Which of these is the correct way to catch errors from an async function getData()?
AgetData().catch(error => console.log(error))
Bcatch(getData())
CgetData().then(error => console.log(error))
DgetData().error()
✗ Incorrect
You attach .catch() directly to the Promise returned by the async function.
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
✗ Incorrect
Unhandled Promise rejections can cause warnings or crashes in Node.js.
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
✗ Incorrect
A single catch at the end of a Promise chain catches errors from any Promise in the chain.
Which Node.js version introduced warnings for unhandled Promise rejections by default?
ANode.js 6
BNode.js 8
CNode.js 10
DNode.js 12
✗ Incorrect
Node.js 10 started showing warnings for unhandled Promise rejections by default.
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.
Practice
(1/5)
1. What is the main purpose of using catch with a Promise in Node.js?
easy
A. To log the successful result of the Promise
B. To start the asynchronous operation
C. To handle errors that occur during the asynchronous operation
D. To convert a Promise into a synchronous function
Solution
Step 1: Understand what catch does in Promises
The catch method is designed to handle any errors that happen during the execution of a Promise or in the chain of Promises.
Step 2: Identify the role of catch in error handling
It catches rejected Promises or exceptions thrown inside then callbacks, preventing unhandled errors.
Final Answer:
To handle errors that occur during the asynchronous operation -> Option C
Quick Check:
Promise catch = error handler [OK]
Hint: Remember: catch always handles errors in promises [OK]
Common Mistakes:
Thinking catch starts the async operation
Confusing catch with success handlers
Believing catch makes code synchronous
2. Which of the following is the correct syntax to catch errors from a Promise named myPromise?
easy
A. myPromise.error(error => console.log(error));
B. myPromise.catch(error => console.log(error));
C. myPromise.then(error => console.log(error));
D. myPromise.finally(error => console.log(error));
Solution
Step 1: Recall the Promise error handling syntax
The correct method to handle errors in a Promise is catch, which takes a callback for the error.
Step 2: Match the syntax with the options
myPromise.catch(error => console.log(error)); uses catch correctly with an arrow function to log the error.
Final Answer:
myPromise.catch(error => console.log(error)); -> Option B
Quick Check:
Use catch for errors in Promises [OK]
Hint: Use .catch() after Promise to handle errors [OK]
Common Mistakes:
Using then to catch errors
Using non-existent error method
Confusing finally with error handling
3. What will be logged to the console when running this code?