0
0
Javascriptprogramming~20 mins

Error handling with async and await in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Error Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of async function with try-catch
What will be the output of this code when run in a modern JavaScript environment?
Javascript
async function test() {
  try {
    await Promise.reject('fail');
  } catch (e) {
    console.log('Caught:', e);
  }
}
test();
Aundefined
BCaught: fail
CUncaught (in promise) fail
DSyntaxError
Attempts:
2 left
💡 Hint
Remember that await inside try-catch can catch rejected promises.
Predict Output
intermediate
2:00remaining
Error propagation in async functions
What will be logged to the console when this code runs?
Javascript
async function foo() {
  throw new Error('Oops');
}

foo().catch(e => console.log(e.message));
AOops
Bundefined
CUncaught Error: Oops
DError: Oops
Attempts:
2 left
💡 Hint
Check how the catch method on the promise handles the error.
Predict Output
advanced
2:00remaining
Handling multiple awaits with error catching
What will be the output of this code snippet?
Javascript
async function process() {
  try {
    await Promise.resolve('First');
    await Promise.reject('Second');
    console.log('Done');
  } catch (err) {
    console.log('Error:', err);
  }
}
process();
AError: Second
BDone
CError: First
DUncaught (in promise) Second
Attempts:
2 left
💡 Hint
The first await resolves, the second rejects and triggers catch.
Predict Output
advanced
2:00remaining
Effect of missing await on error handling
What will be the output of this code?
Javascript
async function run() {
  try {
    Promise.reject('Fail');
  } catch (e) {
    console.log('Caught:', e);
  }
}
run();
ACaught: Fail
Bundefined
CUncaught (in promise) Fail
DSyntaxError
Attempts:
2 left
💡 Hint
Without await, the rejection is not caught by try-catch.
🧠 Conceptual
expert
2:00remaining
Why use try-catch with async/await?
Which statement best explains why try-catch blocks are used with async/await in JavaScript?
ATo prevent the event loop from blocking
BTo make asynchronous code run faster
CTo convert promises into callbacks
DTo handle errors from rejected promises in a synchronous-looking way
Attempts:
2 left
💡 Hint
Think about how async/await changes the way we write asynchronous code.