Challenge - 5 Problems
Async Error Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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();
Attempts:
2 left
💡 Hint
Remember that await inside try-catch can catch rejected promises.
✗ Incorrect
The rejected promise is caught by the catch block, so the console logs 'Caught: fail'.
❓ Predict Output
intermediate2: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));
Attempts:
2 left
💡 Hint
Check how the catch method on the promise handles the error.
✗ Incorrect
The error thrown inside async function foo is caught by the catch method, logging only the message 'Oops'.
❓ Predict Output
advanced2: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();
Attempts:
2 left
💡 Hint
The first await resolves, the second rejects and triggers catch.
✗ Incorrect
The first await resolves successfully, but the second await rejects, so the catch block logs 'Error: Second'.
❓ Predict Output
advanced2: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();
Attempts:
2 left
💡 Hint
Without await, the rejection is not caught by try-catch.
✗ Incorrect
The Promise.reject creates a rejected promise, but without await, the try-catch does not catch it, causing an unhandled rejection.
🧠 Conceptual
expert2:00remaining
Why use try-catch with async/await?
Which statement best explains why try-catch blocks are used with async/await in JavaScript?
Attempts:
2 left
💡 Hint
Think about how async/await changes the way we write asynchronous code.
✗ Incorrect
Try-catch blocks allow handling errors from promises as if the code was synchronous, making it easier to read and maintain.