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
Parallel data fetching
📖 Scenario: You are building a Next.js app that shows user details and their posts on the same page. To make the page load faster, you want to fetch user info and posts at the same time, not one after the other.
🎯 Goal: Create a Next.js page component that fetches user data and posts data in parallel using Promise.all inside getServerSideProps. Then display the user's name and a list of their post titles.
📋 What You'll Learn
Use getServerSideProps to fetch data on the server side
Fetch user data from https://jsonplaceholder.typicode.com/users/1
Fetch posts data from https://jsonplaceholder.typicode.com/posts?userId=1
Fetch both endpoints in parallel using Promise.all
Render the user's name in an <h1> tag
Render the post titles in an unordered list <ul> with each title in a <li>
Use functional React component syntax
💡 Why This Matters
🌍 Real World
Fetching multiple API endpoints in parallel is common in real apps to improve performance and user experience by loading data faster.
💼 Career
Understanding parallel data fetching and server-side rendering is essential for building efficient Next.js applications used in professional web development.
Progress0 / 4 steps
1
Setup basic Next.js page and fetch user data
Create a Next.js page component called UserPage. Inside getServerSideProps, fetch user data from https://jsonplaceholder.typicode.com/users/1 using fetch. Return the user data as a prop named user. Render the user's name inside an <h1> tag in the component.
NextJS
Hint
Use await fetch(url) and then await res.json() to get the user data.
2
Add posts data fetching setup
Inside getServerSideProps, add a fetch call to get posts data from https://jsonplaceholder.typicode.com/posts?userId=1. Store the result in a variable called posts after converting to JSON. Return posts as a prop alongside user. In the component, prepare to render posts but do not add the list yet.
NextJS
Hint
Use await fetch again for posts and convert to JSON.
3
Fetch user and posts data in parallel
Modify getServerSideProps to fetch user and posts data in parallel using Promise.all. Use Promise.all with an array of two fetch calls. Await both responses, then convert each to JSON. Assign the results to user and posts variables respectively. Return both as props.
NextJS
Hint
Use const [resUser, resPosts] = await Promise.all([...]) to fetch both URLs at once.
4
Render the list of post titles
In the UserPage component, below the user's name, render an unordered list <ul>. Inside it, use posts.map to create a list item <li> for each post's title. Make sure each <li> has a unique key using the post's id.
NextJS
Hint
Use posts.map(post => <li key={post.id}>{post.title}</li>) inside the <ul>.
Practice
(1/5)
1. What is the main benefit of using Promise.all for data fetching in Next.js?
easy
A. It fetches multiple data sources at the same time, reducing total wait time.
B. It fetches data sequentially to avoid race conditions.
C. It caches data automatically for offline use.
D. It delays fetching until the user clicks a button.
Solution
Step 1: Understand Promise.all behavior
Promise.all runs multiple promises in parallel and waits for all to finish.
Step 2: Relate to data fetching speed
Fetching multiple data sources at once reduces total waiting time compared to sequential fetching.
Final Answer:
It fetches multiple data sources at the same time, reducing total wait time. -> Option A
Quick Check:
Parallel fetching = faster load [OK]
Hint: Remember: parallel means 'at the same time' [OK]
Common Mistakes:
Thinking Promise.all fetches sequentially
Confusing caching with parallel fetching
Assuming it delays fetching until user action
2. Which of the following is the correct syntax to fetch two APIs in parallel using Promise.all in Next.js?
easy
A. const [res1, res2] = await Promise.all(fetch(url1), fetch(url2));
B. const [res1, res2] = await Promise.all([fetch(url1), fetch(url2)]);
Each fetch returns a Response object with a status property indicating HTTP status.
Step 2: Analyze Promise.all result
Promise.all waits for both fetches and returns an array of Response objects. Logging userRes.status and postsRes.status prints their HTTP status codes.
Final Answer:
200 200 -> Option A
Quick Check:
Response.status = 200 if successful [OK]
Hint: fetch returns Response objects with status property [OK]
Common Mistakes:
Expecting fetch to return JSON directly
Logging promises instead of awaited results
Confusing undefined with Response properties
4. What is wrong with this Next.js code that tries to fetch two APIs in parallel?
A. Nothing is wrong; this code fetches data correctly.
B. The fetch calls are not awaited in parallel, causing sequential fetching.
C. The function should use Promise.all to fetch in parallel.
D. You cannot call json() on a promise; you must await the fetch first.
Solution
Step 1: Identify the type of res1 and res2
Both res1 and res2 are promises from fetch, not Response objects yet.
Step 2: Understand json() usage
You must await the fetch promise to get the Response object before calling json(). Calling json() directly on a promise causes an error.
Final Answer:
You cannot call json() on a promise; you must await the fetch first. -> Option D
Quick Check:
Await fetch before json() [OK]
Hint: Always await fetch before calling json() [OK]
Common Mistakes:
Calling json() on a fetch promise without awaiting
Assuming fetch returns JSON directly
Not using Promise.all for parallelism
5. You want to fetch user info and user posts in parallel in Next.js, but only display posts if user info fetch succeeds. Which approach correctly handles this?
hard
A. Use Promise.all for both fetches and check user info response before rendering posts.
B. Fetch user info first, then fetch posts only if user info fetch succeeds.
C. Use Promise.allSettled to fetch both, then conditionally render posts if user info succeeded.
D. Fetch posts first, then fetch user info regardless of posts fetch result.
Solution
Step 1: Understand requirement for conditional rendering
Posts should display only if user info fetch succeeds, so we need to know each fetch's success independently.
Step 2: Choose correct parallel fetching method
Promise.all fails fast if any promise rejects, so it can't handle partial success. Promise.allSettled waits for all promises and reports each result, allowing conditional logic.
Step 3: Apply conditional rendering logic
After Promise.allSettled, check user info status; if successful, render posts, else skip posts.
Final Answer:
Use Promise.allSettled to fetch both, then conditionally render posts if user info succeeded. -> Option C
Quick Check:
Conditional render needs allSettled [OK]
Hint: Use allSettled to handle partial success in parallel fetches [OK]
Common Mistakes:
Using Promise.all which fails on first error
Fetching posts before confirming user info success