0
0
Javascriptprogramming~20 mins

Promise error handling in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Promise Error Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Promise chain?
Consider the following JavaScript code. What will be printed to the console?
Javascript
Promise.resolve(5)
  .then(value => {
    console.log(value);
    return value * 2;
  })
  .then(value => {
    throw new Error('Oops');
  })
  .catch(error => {
    console.log('Error caught:', error.message);
    return 10;
  })
  .then(value => {
    console.log('Final value:', value);
  });
AError caught: Oops\nFinal value: 10
B5\nError caught: Oops\nFinal value: 10
C5\nFinal value: 10
D5\nError caught: Oops
Attempts:
2 left
💡 Hint
Remember that a catch block handles errors and the chain continues after catch.
Predict Output
intermediate
2:00remaining
What happens if a Promise rejects without a catch?
What will be the output or error when running this code?
Javascript
Promise.reject('Failed')
  .then(() => {
    console.log('Success');
  });
ASyntaxError
BSuccess
CNo output
DUncaught (in promise) Failed
Attempts:
2 left
💡 Hint
Think about what happens when a rejection is not handled.
Predict Output
advanced
2:00remaining
What is the output of this async function with try/catch?
Analyze the following code and select the correct console output.
Javascript
async function test() {
  try {
    await Promise.reject('Error!');
  } catch (e) {
    console.log('Caught:', e);
  }
  console.log('Done');
}
test();
ACaught: Error!\nDone
BDone\nCaught: Error!
CUncaught (in promise) Error!
DSyntaxError
Attempts:
2 left
💡 Hint
Remember that try/catch works with await inside async functions.
Predict Output
advanced
2:00remaining
Which option correctly handles errors in this Promise chain?
Given this code, which option will print 'Error handled' when the Promise rejects?
Javascript
function getData() {
  return new Promise((resolve, reject) => {
    reject('Network error');
  });
}

getData()
  .then(data => console.log('Data:', data))
  // Error handling here
  ;
A.finally(() => console.log('Error handled'))
B.then(error => console.log('Error handled'))
C.catch(error => console.log('Error handled'))
D.then(() => {}, error => console.log('Error handled'))
Attempts:
2 left
💡 Hint
Which method catches rejected Promises?
🧠 Conceptual
expert
3:00remaining
What is the output of this complex Promise error handling?
What will be printed to the console when this code runs?
Javascript
Promise.resolve('Start')
  .then(value => {
    console.log(value);
    return Promise.reject('Fail');
  })
  .catch(error => {
    console.log('Caught:', error);
    return 'Recovered';
  })
  .then(value => {
    console.log(value);
    throw new Error('Crash');
  })
  .catch(error => {
    console.log('Final catch:', error.message);
  });
AStart\nCaught: Fail\nRecovered\nFinal catch: Crash
BStart\nCaught: Fail\nFinal catch: Crash
CStart\nRecovered\nFinal catch: Crash
DStart\nCaught: Fail\nRecovered
Attempts:
2 left
💡 Hint
Follow the chain carefully, noting where errors are caught and what is returned.