Bird
Raised Fist0
Javascriptprogramming~20 mins

Promise chaining in Javascript - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does promise chaining allow you to do in JavaScript?
easy
A. Run multiple synchronous tasks at the same time
B. Stop all asynchronous tasks immediately
C. Run asynchronous tasks one after another, passing results along
D. Convert promises into synchronous functions

Solution

  1. Step 1: Understand the purpose of promise chaining

    Promise chaining is designed to run async tasks in sequence, where each step waits for the previous to finish and can use its result.
  2. Step 2: Compare options with the concept

    Only Run asynchronous tasks one after another, passing results along correctly describes running async tasks one after another and passing results along.
  3. Final Answer:

    Run asynchronous tasks one after another, passing results along -> Option C
  4. Quick Check:

    Promise chaining = sequential async tasks [OK]
Hint: Promise chaining runs async tasks in order [OK]
Common Mistakes:
  • Thinking promises run synchronously
  • Believing chaining stops tasks immediately
  • Confusing promise chaining with parallel execution
2. Which of the following is the correct syntax to chain two promises p1 and p2?
easy
A. p1.then().then(p2).then(result => console.log(result));
B. p1.then(() => p2).then(result => console.log(result));
C. p1.then(p2).then(result => console.log(result));
D. p1.then(result => p2).catch(console.log);

Solution

  1. Step 1: Understand promise chaining syntax

    To chain promises, the first promise's then should return the second promise function call, so it waits for it.
  2. Step 2: Analyze each option

    p1.then(() => p2).then(result => console.log(result)); correctly returns p2 inside then. p1.then(p2).then(result => console.log(result)); passes p2 directly, which is incorrect. p1.then().then(p2).then(result => console.log(result)); has an empty then() which is invalid. p1.then(result => p2).catch(console.log); uses catch prematurely and returns p2 incorrectly.
  3. Final Answer:

    p1.then(() => p2).then(result => console.log(result)); -> Option B
  4. Quick Check:

    Return next promise in then = correct chaining [OK]
Hint: Return next promise inside then() for chaining [OK]
Common Mistakes:
  • Passing promise directly to then instead of a function
  • Leaving then() empty
  • Misplacing catch before chain ends
3. What will be the output of this code?
Promise.resolve(5)
  .then(x => x * 2)
  .then(x => Promise.resolve(x + 3))
  .then(console.log);
medium
A. Error
B. 10
C. Promise { 13 }
D. 13

Solution

  1. Step 1: Trace each then step

    First, Promise.resolve(5) resolves with 5. Then x => x * 2 returns 10. Next, x => Promise.resolve(x + 3) returns a promise resolving to 13.
  2. Step 2: Understand final then output

    The last then(console.log) logs the resolved value 13, not a promise object.
  3. Final Answer:

    13 -> Option D
  4. Quick Check:

    Promise chaining unwraps nested promises [OK]
Hint: Promises unwrap nested promises automatically [OK]
Common Mistakes:
  • Thinking console.log prints a Promise object
  • Ignoring that then unwraps promises
  • Confusing multiplication and addition order
4. Find the error in this promise chain:
fetchData()
  .then(data => processData(data))
  .then(result => console.log(result))
  .catch(error => console.error(error));

function processData(data) {
  throw new Error('Processing failed');
}
medium
A. Error is caught by catch because thrown inside then chain
B. catch should be before then to catch errors
C. Missing return statement in processData causes error
D. Error is not caught because processData is synchronous

Solution

  1. Step 1: Understand error throwing in promise chain

    Throwing an error inside processData causes the promise to reject, which is caught by the catch at the end.
  2. Step 2: Analyze catch placement and behavior

    The catch at the end catches any error thrown in previous then callbacks, including synchronous throws.
  3. Final Answer:

    Error is caught by catch because thrown inside then chain -> Option A
  4. Quick Check:

    Thrown errors in then are caught by catch [OK]
Hint: Errors thrown in then are caught by catch at chain end [OK]
Common Mistakes:
  • Thinking synchronous errors are not caught
  • Placing catch before then incorrectly
  • Assuming missing return causes error
5. You want to run three async tasks task1, task2, and task3 in order, where each depends on the previous result. Which code correctly chains them and catches any error once?
hard
A. task1() .then(result1 => task2(result1)) .then(result2 => task3(result2)) .catch(error => console.error(error));
B. task1() .then(() => task2()) .then(() => task3()) .catch(error => console.error(error));
C. task1() .then(result1 => task2(result1)) .catch(error => console.error(error)) .then(result2 => task3(result2));
D. task1() .catch(error => console.error(error)) .then(result1 => task2(result1)) .then(result2 => task3(result2));

Solution

  1. Step 1: Understand dependency and error handling

    Each task depends on the previous result, so each then must pass the result to the next task function. A single catch at the end handles all errors.
  2. Step 2: Analyze each option

    task1() .then(result1 => task2(result1)) .then(result2 => task3(result2)) .catch(error => console.error(error)); correctly chains tasks with result passing and one catch at the end. task1() .then(() => task2()) .then(() => task3()) .catch(error => console.error(error)); passes task2 and task3 without arguments, which may fail if they expect parameters. task1() .then(result1 => task2(result1)) .catch(error => console.error(error)) .then(result2 => task3(result2)); places catch too early, breaking the chain. task1() .catch(error => console.error(error)) .then(result1 => task2(result1)) .then(result2 => task3(result2)); places catch before then, so errors in later tasks are not caught.
  3. Final Answer:

    task1().then(result1 => task2(result1)).then(result2 => task3(result2)).catch(error => console.error(error)); -> Option A
  4. Quick Check:

    Chain with single catch at end and pass results [OK]
Hint: Chain with one catch at end, pass results explicitly [OK]
Common Mistakes:
  • Placing catch too early breaking chain
  • Not passing results to dependent tasks
  • Multiple catches causing partial error handling
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard