0
0
Node.jsframework~20 mins

Promise catch for async errors in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Promise Catch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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));
AUncaught (in promise) Error occurred
BCaught: Error occurred
CNothing is logged
DSyntaxError
Attempts:
2 left
💡 Hint
Remember that .catch handles rejected promises.
📝 Syntax
intermediate
2: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?
AfetchData().catch(err => console.log('Error:', err.message));
BfetchData().catch(err => console.log('Success:', err.message));
Ctry { fetchData(); } catch(err) { console.log('Error:', err.message); }
DfetchData().then(err => console.log('Error:', err.message));
Attempts:
2 left
💡 Hint
Promises use .catch to handle errors, not .then or try/catch directly.
🔧 Debug
advanced
2: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));
AThe promise is never rejected explicitly, so .catch does not run.
BThe .catch block is missing a return statement.
CThe setTimeout callback prevents the promise from resolving.
DThe error is thrown outside the promise executor, so it is uncaught by .catch.
Attempts:
2 left
💡 Hint
Errors thrown asynchronously inside setTimeout are not caught by the promise unless rejected explicitly.
state_output
advanced
2: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';
  });
Astart-then1-then2
Bstart-then1-catch-then2
Cstart-then1-catch-recovered-then2
Dcatch-recovered-then2
Attempts:
2 left
💡 Hint
Remember that catch returns a value that the next then receives.
🧠 Conceptual
expert
2: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.
APromise.catch handles rejected promises, while async/await requires try/catch blocks to handle errors.
BPromise.catch automatically retries the async function on error, async/await does not.
CAsync/await does not support error handling; only Promise.catch can catch errors.
DPromise.catch can only catch synchronous errors, async/await catches asynchronous errors.
Attempts:
2 left
💡 Hint
Think about how errors are caught in promises vs async/await syntax.