Challenge - 5 Problems
Promise Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Promise chaining with then and catch
What is the output of this code snippet?
Javascript
Promise.resolve(5) .then(x => x * 2) .then(x => { throw new Error('Oops'); }) .catch(e => 'Error caught') .then(x => x + ' done') .then(console.log);
Attempts:
2 left
💡 Hint
Remember catch handles errors and passes its return value to the next then.
✗ Incorrect
The promise resolves with 5, then multiplied by 2 gives 10. Then an error is thrown, which is caught by catch returning 'Error caught'. The next then appends ' done' to that string, resulting in 'Error caught done'.
❓ Predict Output
intermediate2:00remaining
Error propagation in promise chains
What will be logged by this code?
Javascript
Promise.reject('fail') .then(() => 'success') .catch(err => err) .then(console.log);
Attempts:
2 left
💡 Hint
Catch returns the error string, which is passed to the next then.
✗ Incorrect
The promise is rejected with 'fail'. The then is skipped. The catch receives 'fail' and returns it. The next then logs 'fail'.
❓ Predict Output
advanced2:00remaining
Multiple catch blocks in promise chain
What is the output of this code?
Javascript
Promise.resolve('start') .then(val => { throw 'error1'; }) .catch(err => { console.log('Caught:', err); throw 'error2'; }) .catch(err => console.log('Caught again:', err));
Attempts:
2 left
💡 Hint
A catch can throw again to be caught by another catch.
✗ Incorrect
The first then throws 'error1', caught by first catch which logs it and throws 'error2'. The second catch catches 'error2' and logs it.
🧠 Conceptual
advanced2:00remaining
Promise then return values
Which option correctly describes what happens when a then callback returns a promise?
Attempts:
2 left
💡 Hint
Think about how promise chaining works with promises returned inside then.
✗ Incorrect
When a then returns a promise, the next then waits for that promise to resolve and receives its resolved value.
❓ Predict Output
expert3:00remaining
Complex promise chain with nested then and catch
What is the final output logged by this code?
Javascript
Promise.resolve(1) .then(x => x + 1) .then(x => { return Promise.reject('fail at step 3'); }) .catch(err => { console.log('Caught:', err); return 10; }) .then(x => x * 2) .then(x => { throw 'final error'; }) .catch(err => console.log('Final catch:', err));
Attempts:
2 left
💡 Hint
Follow the chain carefully, noting where errors are caught and what values are returned.
✗ Incorrect
The promise resolves with 1, then adds 1 to get 2. Then it rejects with 'fail at step 3'. The catch logs 'Caught: fail at step 3' and returns 10. Next then multiplies 10 by 2 to get 20. Then throws 'final error'. The last catch logs 'Final catch: final error'.