📖 Scenario: You are building a Next.js app that shows user details and their posts side by side. To make the page load faster, you want to fetch user info and posts at the same time, not one after the other.
🎯 Goal: Build a Next.js server component that fetches user data and posts data in parallel using Promise.all and displays the user name and the titles of their posts.
📋 What You'll Learn
Create two async functions to fetch user data and posts data
Use a config variable for the user ID
Fetch both user and posts data in parallel using Promise.all
Render the user name and a list of post titles in the component
💡 Why This Matters
🌍 Real World
Fetching multiple data sources in parallel is common in web apps to improve performance and user experience.
💼 Career
Understanding parallel data fetching and server components is essential for building fast, scalable Next.js applications.
Progress0 / 4 steps
1
Create async functions to fetch user and posts data
Create two async functions called fetchUser and fetchPosts. fetchUser should fetch from https://jsonplaceholder.typicode.com/users/1. fetchPosts should fetch from https://jsonplaceholder.typicode.com/posts?userId=1. Both should return the JSON response.
NextJS
Hint
Use async function syntax and await fetch(...) to get data, then call res.json() to parse it.
2
Add a config variable for user ID
Create a constant called userId and set it to 1. Update the URLs in fetchUser and fetchPosts to use this userId variable instead of the hardcoded number.
NextJS
Hint
Use a template string with backticks and ${userId} to insert the variable into the URL.
3
Fetch user and posts data in parallel
Create an async function called getData. Inside it, use Promise.all to run fetchUser() and fetchPosts() in parallel. Return an object with user and posts properties holding the results.
NextJS
Hint
Use array destructuring to get user and posts from the array returned by Promise.all.
4
Create the Next.js server component to render user and posts
Create an async React component called Page. Inside it, call getData() to get user and posts. Return JSX that renders the user's name inside an <h1> and a list of post title inside <li> elements within a <ul>.
NextJS
Hint
Remember to use await when calling getData() inside the async component. Use map to render the list of posts.
Practice
(1/5)
1. What is the main benefit of using the Parallel data fetching pattern in Next.js?
easy
A. It delays fetching data until the user clicks a button.
B. It fetches multiple data sources at the same time to reduce loading time.
C. It fetches data one after another to ensure order.
D. It caches data permanently on the server.
Solution
Step 1: Understand parallel fetching concept
Parallel fetching means requesting multiple data sources simultaneously rather than waiting for one to finish before starting the next.
Step 2: Identify the benefit in Next.js context
Fetching data in parallel reduces total wait time, improving page load speed and user experience.
Final Answer:
It fetches multiple data sources at the same time to reduce loading time. -> Option B
Quick Check:
Parallel fetching = faster loading [OK]
Hint: Parallel means multiple fetches run together, not one by one [OK]
Common Mistakes:
Thinking parallel means sequential fetching
Confusing caching with parallel fetching
Assuming parallel fetching waits for user action
2. Which of the following is the correct syntax to fetch two APIs in parallel using Next.js with Promise.all?
easy
A. const [data1, data2] = await Promise.all([fetch(url1), fetch(url2)]);
A. You cannot use Promise.all with fetch in Next.js.
B. fetch calls must be awaited before Promise.all.
C. Promise.all expects an array, but data1 and data2 are passed as separate arguments.
D. The function should not be async.
Solution
Step 1: Check Promise.all argument
Promise.all requires a single array of promises, but here two promises are passed as separate arguments.
Step 2: Correct usage
It should be Promise.all([data1, data2]) to run fetches in parallel correctly.
Final Answer:
Promise.all expects an array, but data1 and data2 are passed as separate arguments. -> Option C
Quick Check:
Promise.all needs array argument [OK]
Hint: Always pass an array to Promise.all, not separate arguments [OK]
Common Mistakes:
Passing multiple arguments instead of an array to Promise.all
Awaiting fetch before Promise.all (sequential fetch)
Misunderstanding async function requirements
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 pattern correctly handles this?
hard
A. Fetch user info first, then fetch posts only if user info fetch succeeds.
B. Use Promise.all to fetch both, then check user info before rendering posts.
C. Fetch posts first, then fetch user info regardless of posts result.
D. Fetch both sequentially without checking results.
Solution
Step 1: Understand dependency between data
Posts depend on user info, so user info must be fetched and verified first.
Step 2: Choose correct fetching order
Fetch user info first; if successful, then fetch posts. This avoids unnecessary posts fetch if user info fails.
Final Answer:
Fetch user info first, then fetch posts only if user info fetch succeeds. -> Option A
Quick Check:
Dependent fetches require sequential order [OK]
Hint: Fetch dependent data sequentially, independent data in parallel [OK]
Common Mistakes:
Fetching dependent data in parallel without checks
Ignoring fetch failure conditions
Fetching unnecessary data causing wasted resources
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.