Challenge - 5 Problems
Promise Chaining Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this promise chain?
Consider the following JavaScript code using promise chaining. What will be printed to the console?
Javascript
Promise.resolve(5) .then(value => value * 2) .then(value => { console.log(value); return value + 3; }) .then(value => console.log(value));
Attempts:
2 left
💡 Hint
Remember each .then receives the resolved value from the previous .then.
✗ Incorrect
The first then doubles 5 to 10. The second then logs 10 and returns 13. The last then logs 13.
❓ Predict Output
intermediate2:00remaining
What does this promise chain output?
Look at this code snippet. What will it print to the console?
Javascript
Promise.resolve('start') .then(str => str + ' middle') .then(str => { throw new Error('Oops'); }) .catch(err => 'caught error') .then(str => console.log(str));
Attempts:
2 left
💡 Hint
Check how errors are caught and handled in promise chains.
✗ Incorrect
The error thrown is caught by the catch, which returns 'caught error'. The last then logs that string.
❓ Predict Output
advanced2:00remaining
What is the final value logged?
Analyze this promise chain and determine the final value logged to the console.
Javascript
Promise.resolve(1) .then(x => x + 1) .then(x => Promise.resolve(x + 1)) .then(x => { throw 'error'; }) .catch(() => 10) .then(x => x + 1) .then(x => console.log(x));
Attempts:
2 left
💡 Hint
Remember catch returns a resolved promise with 10, then the chain continues.
✗ Incorrect
The chain throws an error, caught by catch returning 10, then adds 1, logging 11.
❓ Predict Output
advanced2:00remaining
What error does this promise chain produce?
What error will this code produce when run?
Javascript
Promise.resolve(3) .then(x => x * 2) .then(() => { throw new TypeError('Wrong type'); }) .then(x => x + 1) .catch(err => { console.log(err.name); throw err; }) .then(() => console.log('Done')) .catch(err => console.log('Final catch:', err.message));
Attempts:
2 left
💡 Hint
Look at how errors propagate and how catch rethrows the error.
✗ Incorrect
The first catch logs 'TypeError' and rethrows. The next then is skipped. The final catch logs the error message.
🧠 Conceptual
expert3:00remaining
Which option correctly chains promises to fetch data sequentially?
You want to fetch user data, then fetch posts for that user, and finally fetch comments for the first post. Which promise chain correctly does this?
Attempts:
2 left
💡 Hint
Remember that to chain dependent async calls, you must return the inner promise inside then.
✗ Incorrect
Option B correctly chains the promises sequentially by returning each promise in the chain, allowing fetchComments to wait for fetchPosts, and finally logging comments.