Bird
Raised Fist0
NextJSframework~10 mins

Sequential data fetching in NextJS - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to fetch data sequentially inside a React Server Component.

NextJS
export default async function Page() {
  const data1 = await fetch('/api/data1').then(res => res.json());
  const data2 = await [1]('/api/data2').then(res => res.json());
  return <div>{data1.title} - {data2.title}</div>;
}
Drag options to blanks, or click blank then click option'
Afetch
Baxios
CgetServerSideProps
DuseEffect
Attempts:
3 left
💡 Hint
Common Mistakes
Using client-side hooks like useEffect in Server Components.
Using axios without importing or in Server Components.
2fill in blank
medium

Complete the code to fetch two APIs one after another and combine their results.

NextJS
export default async function Page() {
  const res1 = await fetch('/api/user');
  const user = await res1.json();
  const res2 = await fetch(`/api/posts?userId=${user.id}`);
  const posts = await [1].json();
  return <main>{user.name} has {posts.length} posts.</main>;
}
Drag options to blanks, or click blank then click option'
Ares1
Buser
Cres2
Dposts
Attempts:
3 left
💡 Hint
Common Mistakes
Calling .json() on the wrong variable.
Trying to parse JSON before awaiting the fetch.
3fill in blank
hard

Fix the error in the code to fetch data sequentially and render it.

NextJS
export default async function Page() {
  const data1 = await fetch('/api/data1').then(res => res.json());
  const data2 = [1] fetch('/api/data2').then(res => res.json());
  return <div>{data1.name} and {data2.name}</div>;
}
Drag options to blanks, or click blank then click option'
Aawait
Basync
Cthen
Dreturn
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to await asynchronous fetch calls.
Trying to access properties on a Promise.
4fill in blank
hard

Fill both blanks to create a sequential fetch and parse JSON for two APIs.

NextJS
export default async function Page() {
  const response1 = await fetch('/api/info');
  const info = await response1[1]();
  const response2 = await fetch('/api/details');
  const details = await response2[2]();
  return <section>{info.title} - {details.description}</section>;
}
Drag options to blanks, or click blank then click option'
A.json
B.text
C.blob
D.arrayBuffer
Attempts:
3 left
💡 Hint
Common Mistakes
Using .text() or .blob() instead of .json() when expecting JSON data.
Forgetting to await the parsing method.
5fill in blank
hard

Fill all three blanks to fetch user, then posts, then comments sequentially and parse JSON.

NextJS
export default async function Page() {
  const userRes = await fetch('/api/user');
  const user = await userRes[1]();
  const postsRes = await fetch(`/api/posts?userId=${user.id}`);
  const posts = await postsRes[2]();
  const commentsRes = await fetch(`/api/comments?postId=${posts[0].id}`);
  const comments = await commentsRes[3]();
  return <article>{user.name} has {posts.length} posts and {comments.length} comments on the first post.</article>;
}
Drag options to blanks, or click blank then click option'
A.json
B.text
C.blob
D.arrayBuffer
Attempts:
3 left
💡 Hint
Common Mistakes
Using different parsing methods inconsistently.
Not awaiting the parsing methods.

Practice

(1/5)
1. What is the main reason to use sequential data fetching in Next.js?
easy
A. Because later data depends on the results of earlier data
B. To fetch all data at the same time for speed
C. To avoid using async/await syntax
D. To fetch data only once when the app starts

Solution

  1. Step 1: Understand the dependency in data fetching

    Sequential fetching is used when one piece of data needs the result of a previous fetch to continue.
  2. Step 2: Compare with other fetching methods

    Parallel fetching gets all data at once, but sequential waits for each step because of dependency.
  3. Final Answer:

    Because later data depends on the results of earlier data -> Option A
  4. Quick Check:

    Sequential fetching = dependent data steps [OK]
Hint: Use sequential fetching when data depends on previous results [OK]
Common Mistakes:
  • Thinking parallel fetching is always better
  • Ignoring data dependencies
  • Confusing async/await with parallel fetching
2. Which of the following is the correct syntax to fetch data sequentially using async/await in Next.js?
easy
A. const data1 = fetch(url1); const data2 = fetch(url2);
B. const data1 = await fetch(url1), const data2 = await fetch(url2);
C. const data1 = fetch(url1).then(); const data2 = fetch(url2).then();
D. const data1 = await fetch(url1); const data2 = await fetch(url2);

Solution

  1. Step 1: Identify correct async/await usage

    Using await before fetch pauses execution until the promise resolves, ensuring sequential order.
  2. Step 2: Check syntax correctness

    const data1 = await fetch(url1); const data2 = await fetch(url2); uses two separate await statements correctly. const data1 = await fetch(url1), const data2 = await fetch(url2); has a syntax error with comma instead of semicolon.
  3. Final Answer:

    const data1 = await fetch(url1); const data2 = await fetch(url2); -> Option D
  4. Quick Check:

    Separate awaits with semicolons = correct syntax [OK]
Hint: Use separate await statements with semicolons for sequential fetch [OK]
Common Mistakes:
  • Using commas instead of semicolons between await calls
  • Not using await causing parallel fetch
  • Using then() without await for sequential logic
3. What will be the output of this Next.js code snippet?
async function fetchData() {
  const res1 = await fetch('https://api.example.com/user');
  const user = await res1.json();
  const res2 = await fetch(`https://api.example.com/posts?userId=${user.id}`);
  const posts = await res2.json();
  return posts.length;
}
medium
A. Always zero because fetch is asynchronous
B. An error because user.id is undefined
C. The number of posts for the fetched user
D. The total number of users

Solution

  1. Step 1: Understand sequential fetch calls

    The first fetch gets user data, then uses user.id to fetch posts for that user.
  2. Step 2: Analyze returned value

    The function returns posts.length, which is the count of posts for that user.
  3. Final Answer:

    The number of posts for the fetched user -> Option C
  4. Quick Check:

    Sequential fetch returns posts count [OK]
Hint: Check if later fetch depends on earlier data for output [OK]
Common Mistakes:
  • Assuming user.id is undefined without checking response
  • Confusing posts length with total users
  • Thinking async fetch returns zero immediately
4. Identify the error in this sequential data fetching code in Next.js:
async function getData() {
  const user = fetch('/api/user');
  const posts = await fetch(`/api/posts?userId=${user.id}`);
  return posts.json();
}
medium
A. Incorrect URL format in second fetch
B. Missing await before first fetch causing user to be a Promise
C. Missing await before posts.json()
D. Using template literals incorrectly

Solution

  1. Step 1: Check fetch usage for user

    fetch returns a Promise; without await, user is a Promise, not the data object.
  2. Step 2: Understand impact on user.id

    Accessing user.id fails because user is not resolved yet, causing runtime error.
  3. Final Answer:

    Missing await before first fetch causing user to be a Promise -> Option B
  4. Quick Check:

    Always await fetch to get resolved data [OK]
Hint: Always await fetch before accessing response data [OK]
Common Mistakes:
  • Forgetting await before fetch
  • Assuming fetch returns data directly
  • Not awaiting json() call
5. You want to fetch user info, then fetch their orders, and finally fetch details for each order sequentially in Next.js. Which approach correctly handles this?
hard
A. Use async/await to fetch user, then orders, then loop with await inside for-of to fetch each order detail
B. Fetch user and orders in parallel, then fetch order details in parallel with Promise.all
C. Fetch user, then orders, then map order details fetches without await inside map
D. Fetch all data at once without waiting for previous fetches

Solution

  1. Step 1: Understand sequential dependency

    Order details depend on orders, which depend on user info, so fetches must be sequential.
  2. Step 2: Use for-of with await for sequential order detail fetch

    Using for-of with await inside ensures each order detail fetch waits for the previous to finish.
  3. Final Answer:

    Use async/await to fetch user, then orders, then loop with await inside for-of to fetch each order detail -> Option A
  4. Quick Check:

    Sequential fetch with for-of and await = correct approach [OK]
Hint: Use for-of with await for sequential loops in async functions [OK]
Common Mistakes:
  • Using map without await causing parallel fetch
  • Fetching all data at once ignoring dependencies
  • Mixing parallel and sequential fetch incorrectly