Challenge - 5 Problems
Promise Catch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when a rejected promise is caught?
Consider this Node.js code snippet using promises. What will be logged to the console?
Node.js
const promise = new Promise((resolve, reject) => {
reject('Error occurred');
});
promise.catch(error => console.log('Caught:', error));Attempts:
2 left
💡 Hint
Remember that .catch handles rejected promises.
✗ Incorrect
The promise is rejected with 'Error occurred'. The .catch method catches this rejection and logs the message with 'Caught:'.
📝 Syntax
intermediate2:00remaining
Which option correctly catches errors from an async function?
Given an async function that may throw an error, which code snippet correctly catches the error using promises?
Node.js
async function fetchData() { throw new Error('Failed to fetch'); } // Which option correctly catches the error?
Attempts:
2 left
💡 Hint
Promises use .catch to handle errors, not .then or try/catch directly.
✗ Incorrect
Calling fetchData() returns a promise. Using .catch handles any rejection. Option A correctly logs the error message.
🔧 Debug
advanced2:00remaining
Why does this promise catch block not handle the error?
Look at this code. Why is the error not caught by the .catch block?
Node.js
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
throw new Error('Delayed error');
}, 100);
});
promise.catch(err => console.log('Caught:', err.message));Attempts:
2 left
💡 Hint
Errors thrown asynchronously inside setTimeout are not caught by the promise unless rejected explicitly.
✗ Incorrect
Throwing an error inside setTimeout does not reject the promise. The promise executor finishes without rejection, so .catch is never triggered.
❓ state_output
advanced2:00remaining
What is the final value of 'result' after promise chaining with catch?
Analyze this code and determine the final value of the variable 'result'.
Node.js
let result = ''; Promise.resolve('start') .then(value => { result += value + '-then1-'; throw new Error('fail'); }) .catch(err => { result += 'catch-'; return 'recovered'; }) .then(value => { result += value + '-then2'; });
Attempts:
2 left
💡 Hint
Remember that catch returns a value that the next then receives.
✗ Incorrect
The first then appends 'start-then1-' then throws. The catch appends 'catch-' and returns 'recovered'. The next then appends 'recovered-then2'.
🧠 Conceptual
expert2:00remaining
Which statement about Promise.catch and async/await error handling is true?
Select the correct statement about how Promise.catch and async/await handle errors in Node.js.
Attempts:
2 left
💡 Hint
Think about how errors are caught in promises vs async/await syntax.
✗ Incorrect
Promise.catch is a method to handle rejected promises. Async/await syntax requires try/catch blocks to handle errors thrown inside async functions.