0
0
NextJSframework~20 mins

Parallel data fetching in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Parallel Fetching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the rendered output of this Next.js component?

Consider this Next.js component that fetches user and post data in parallel using Promise.all. What will it render?

NextJS
import React from 'react';

async function fetchUser() {
  return { name: 'Alice' };
}

async function fetchPosts() {
  return ['Post1', 'Post2'];
}

export default async function Page() {
  const [user, posts] = await Promise.all([fetchUser(), fetchPosts()]);
  return (
    <main>
      <h1>User: {user.name}</h1>
      <ul>
        {posts.map((post, i) => <li key={i}>{post}</li>)}
      </ul>
    </main>
  );
}
A<main><h1>User: undefined</h1><ul><li>Post1</li><li>Post2</li></ul></main>
BRuntime error: Cannot read property 'map' of undefined
C<main><h1>User: Alice</h1><ul></ul></main>
D<main><h1>User: Alice</h1><ul><li>Post1</li><li>Post2</li></ul></main>
Attempts:
2 left
💡 Hint

Think about what Promise.all returns and how the data is destructured.

📝 Syntax
intermediate
2:00remaining
Which option correctly fetches data in parallel in a Next.js server component?

Choose the code snippet that correctly fetches data1 and data2 in parallel inside a Next.js server component.

Aconst [data1, data2] = await Promise.all([fetch('/api/data1'), fetch('/api/data2')]);
B
const data1 = await fetch('/api/data1');
const data2 = await fetch('/api/data2');
C
const data1 = fetch('/api/data1');
const data2 = fetch('/api/data2');
await data1;
await data2;
D
const data1 = fetch('/api/data1').then(res =&gt; res.json());
const data2 = fetch('/api/data2').then(res =&gt; res.json());
Attempts:
2 left
💡 Hint

Remember that Promise.all runs promises in parallel and waits for all to finish.

🔧 Debug
advanced
2:00remaining
Why does this parallel fetch code cause a runtime error?

Examine this Next.js server component code snippet. It tries to fetch two APIs in parallel but causes a runtime error. What is the cause?

NextJS
export default async function Page() {
  const data1 = fetch('/api/data1').then(res => res.json());
  const data2 = fetch('/api/data2').then(res => res.json());
  return (
    <main>
      <p>Data1: {data1.value}</p>
      <p>Data2: {data2.value}</p>
    </main>
  );
}
Adata1 and data2 are promises, not resolved data, so accessing .value causes an error.
Bfetch calls are missing await, causing a syntax error.
CThe component must be a client component to use fetch.
DThe API endpoints '/api/data1' and '/api/data2' do not exist, causing a 404 error.
Attempts:
2 left
💡 Hint

Check what fetch(...).then(...) returns and how it is used in JSX.

state_output
advanced
2:00remaining
What is the output of this React component using parallel data fetching with hooks?

Consider this React functional component that fetches two APIs in parallel using useEffect and Promise.all. What will it display after data loads?

NextJS
import React, { useState, useEffect } from 'react';

export default function DataDisplay() {
  const [data, setData] = useState({ user: null, posts: null });

  useEffect(() => {
    Promise.all([
      fetch('/api/user').then(res => res.json()),
      fetch('/api/posts').then(res => res.json())
    ]).then(([userData, postsData]) => {
      setData({ user: userData, posts: postsData });
    });
  }, []);

  if (!data.user || !data.posts) return <p>Loading...</p>;

  return (
    <div>
      <h2>{data.user.name}</h2>
      <ul>
        {data.posts.map(post => <li key={post.id}>{post.title}</li>)}
      </ul>
    </div>
  );
}
A<p>Loading...</p> forever because state never updates.
B<p>Loading...</p> initially, then user name and list of post titles after fetches complete.
CAn error because <code>data.posts</code> is null when mapping.
DUser name displayed but posts list is empty.
Attempts:
2 left
💡 Hint

Think about how useEffect and state updates work asynchronously.

🧠 Conceptual
expert
2:00remaining
Why is parallel data fetching preferred over sequential fetching in Next.js server components?

Choose the best explanation for why fetching multiple data sources in parallel is better than fetching them one after another in Next.js server components.

ASequential fetching allows better error handling and is preferred for reliability.
BSequential fetching is always faster because it avoids network congestion.
CParallel fetching reduces total wait time by running requests simultaneously, improving performance and user experience.
DParallel fetching uses more memory and should be avoided to reduce server load.
Attempts:
2 left
💡 Hint

Think about how waiting for multiple tasks works in real life, like waiting in line versus doing tasks at the same time.