Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is promise chaining in JavaScript?
Promise chaining is a way to run multiple asynchronous tasks one after another, where each task starts when the previous one finishes, using .then() methods.
Click to reveal answer
beginner
How do you return a value from one promise to the next in a chain?
You return the value inside the .then() callback. This value becomes the input for the next .then() in the chain.
Click to reveal answer
intermediate
What happens if a promise in the chain is rejected?
If a promise is rejected, the chain skips to the nearest .catch() handler to handle the error.
Click to reveal answer
beginner
Why is promise chaining better than nested callbacks?
Promise chaining avoids deeply nested code (callback hell), making code easier to read and maintain by keeping asynchronous steps in a flat, linear structure.
Click to reveal answer
beginner
Show a simple example of promise chaining with two asynchronous steps.
This runs fetchData, then processData with the fetched data, then logs the result, handling errors if any.
Click to reveal answer
What method do you use to add the next step in a promise chain?
A.then()
B.catch()
C.finally()
D.start()
✗ Incorrect
The .then() method adds the next step to a promise chain.
If a promise in the chain fails, which method handles the error?
A.then()
B.finally()
C.error()
D.catch()
✗ Incorrect
The .catch() method handles errors in promise chains.
What does returning a promise inside a .then() do?
AEnds the chain
BWaits for that promise before continuing the chain
CStarts a new unrelated chain
DIgnores the returned promise
✗ Incorrect
Returning a promise inside .then() makes the chain wait for it to resolve before moving on.
Which problem does promise chaining help to solve?
ACallback hell
BMemory leaks
CSynchronous blocking
DVariable hoisting
✗ Incorrect
Promise chaining helps avoid deeply nested callbacks known as callback hell.
What will this code output?
Promise.resolve(5)
.then(x => x * 2)
.then(x => x + 1)
.then(console.log);
A5
B10
C11
DError
✗ Incorrect
The chain multiplies 5 by 2 (10), then adds 1 (11), then logs 11.
Explain how promise chaining works and why it is useful.
Think about how you do tasks one after another and how promises help with that.
You got /4 concepts.
Write a simple promise chain that fetches data, processes it, and logs the result, including error handling.
Start with a promise, then add steps with .then(), and end with .catch()
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of promise chaining in Node.js?
easy
A. To convert synchronous code into asynchronous code
B. To run all asynchronous tasks at the same time
C. To run asynchronous tasks one after another in order
D. To stop all asynchronous tasks immediately
Solution
Step 1: Understand asynchronous task execution
Promise chaining allows tasks to run one after another, waiting for each to finish.
Step 2: Identify the purpose of chaining
Chaining with .then() ensures order, not parallel or immediate stop.
Final Answer:
To run asynchronous tasks one after another in order -> Option C
Quick Check:
Promise chaining = ordered async tasks [OK]
Hint: Promise chaining runs tasks step-by-step, not all at once [OK]
Common Mistakes:
Thinking promises run in parallel by default
Confusing chaining with synchronous loops
Believing chaining stops tasks immediately
2. Which of the following is the correct syntax to chain two promises promise1 and promise2?
easy
A. promise1.then(promise2)
B. promise1.then(promise2())
C. promise1.then.then(promise2)
D. promise1.then(() => promise2())
Solution
Step 1: Understand how to pass functions to .then()
The .then() method expects a function to call when the promise resolves.
Step 2: Check each option's syntax
promise1.then(() => promise2()) correctly passes a function that calls promise2(). Options A and B call promise2() immediately or pass wrong types. promise1.then.then(promise2) has invalid chaining syntax.
Final Answer:
promise1.then(() => promise2()) -> Option D
Quick Check:
Pass a function to then() = promise1.then(() => promise2()) [OK]
Hint: Use a function inside then() to delay calling next promise [OK]
Common Mistakes:
Calling the next promise immediately inside then()
Using double then without parentheses
Passing promise instead of function to then()
3. What will be logged to the console when running this code?