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
Promise chaining
📖 Scenario: You are building a simple Node.js program that simulates fetching user data and then fetching that user's posts from a server. Each fetch returns a Promise that resolves after a short delay.
🎯 Goal: Build a chain of Promises where you first fetch a user, then fetch the posts of that user, and finally log the posts.
📋 What You'll Learn
Create a function fetchUser that returns a Promise resolving to the user object { id: 1, name: 'Alice' } after 100ms.
Create a function fetchPosts that takes a user ID and returns a Promise resolving to an array of posts [{ id: 1, title: 'Hello World' }, { id: 2, title: 'Learning Promises' }] after 100ms.
Create a variable userId to store the fetched user's ID.
Chain the Promises so that fetchUser is called first, then fetchPosts is called with the userId, and finally the posts are logged.
💡 Why This Matters
🌍 Real World
Promise chaining is used in Node.js to handle sequences of asynchronous tasks, such as fetching data from APIs or databases in order.
💼 Career
Understanding Promise chaining is essential for backend developers working with asynchronous JavaScript, enabling them to write clean, readable, and maintainable code.
Progress0 / 4 steps
1
Create the fetchUser function
Create a function called fetchUser that returns a Promise. This Promise should resolve after 100 milliseconds with the object { id: 1, name: 'Alice' }.
Node.js
Hint
Use new Promise and setTimeout to simulate a delayed response.
2
Create the fetchPosts function
Create a function called fetchPosts that takes a parameter userId and returns a Promise. This Promise should resolve after 100 milliseconds with the array [{ id: 1, title: 'Hello World' }, { id: 2, title: 'Learning Promises' }].
Node.js
Hint
Use userId as a parameter but you don't need to use it inside the function body for this example.
3
Create the userId variable
Create a variable called userId and set it to null. This will hold the ID of the fetched user.
Node.js
Hint
Use let to declare userId and set it to null.
4
Chain the Promises to fetch user and posts
Use fetchUser() and chain a .then() to get the user object. Inside this .then(), set userId to the user's id and return fetchPosts(userId). Chain another .then() to receive the posts and log them using console.log(posts).
Node.js
Hint
Remember to return fetchPosts(userId) inside the first .then() to chain the Promises.
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?