0
0
Javascriptprogramming~20 mins

Promise chaining in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Promise Chaining 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 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));
A5\n8
B13\n16
C10\n10
D10\n13
Attempts:
2 left
💡 Hint
Remember each .then receives the resolved value from the previous .then.
Predict Output
intermediate
2: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));
Acaught error
Bstart middle
COops
Dundefined
Attempts:
2 left
💡 Hint
Check how errors are caught and handled in promise chains.
Predict Output
advanced
2: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));
A11
B4
Cerror
Dundefined
Attempts:
2 left
💡 Hint
Remember catch returns a resolved promise with 10, then the chain continues.
Predict Output
advanced
2: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));
ATypeError\nDone
BWrong type\nDone
CTypeError\nFinal catch: Wrong type
DSyntaxError
Attempts:
2 left
💡 Hint
Look at how errors propagate and how catch rethrows the error.
🧠 Conceptual
expert
3: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?
AfetchUser().then(user => fetchPosts(user.id).then(posts => fetchComments(posts[0].id))).then(comments => console.log(comments));
BfetchUser().then(user => fetchPosts(user.id)).then(posts => fetchComments(posts[0].id)).then(comments => console.log(comments));
CfetchUser().then(user => fetchPosts(user.id)); fetchComments(posts[0].id).then(comments => console.log(comments));
DfetchUser().then(user => fetchPosts(user.id)); then(posts => fetchComments(posts[0].id)).then(comments => console.log(comments));
Attempts:
2 left
💡 Hint
Remember that to chain dependent async calls, you must return the inner promise inside then.