0
0
NextJSframework~20 mins

Parallel data fetching pattern in NextJS - Practice Problems & Coding Challenges

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

Consider this Next.js component that fetches two APIs in parallel using Promise.all. What will it render?

NextJS
import React from 'react';

export default async function Page() {
  const [postsRes, usersRes] = await Promise.all([
    fetch('https://jsonplaceholder.typicode.com/posts'),
    fetch('https://jsonplaceholder.typicode.com/users')
  ]);
  const posts = await postsRes.json();
  const users = await usersRes.json();

  return (
    <main>
      <h1>Posts count: {posts.length}</h1>
      <h2>First user name: {users[0].name}</h2>
    </main>
  );
}
ASyntaxError due to invalid await usage in component
B<main><h1>Posts count: 0</h1><h2>First user name: undefined</h2></main>
C<main><h1>Posts count: 100</h1><h2>First user name: Leanne Graham</h2></main>
DRuntime error: Cannot read property 'length' of undefined
Attempts:
2 left
💡 Hint

Think about how Promise.all helps fetch data simultaneously and what the API returns.

📝 Syntax
intermediate
2:00remaining
Which option correctly uses parallel data fetching in a Next.js Server Component?

Choose the code snippet that correctly fetches two APIs in parallel inside a Next.js Server Component.

Aconst [data1, data2] = await Promise.all([fetch('/api/data1'), fetch('/api/data2')]);
Bconst data1 = fetch('/api/data1').then(res => res.json()); const data2 = fetch('/api/data2').then(res => res.json());
Cconst data1 = fetch('/api/data1'); const data2 = fetch('/api/data2'); await data1; await data2;
Dconst data1 = await fetch('/api/data1'); const data2 = await fetch('/api/data2');
Attempts:
2 left
💡 Hint

Look for the option that starts both fetches at the same time and waits for both to finish.

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

Examine this code snippet inside a Next.js Server Component. Why does it cause a runtime error?

NextJS
const [posts, users] = await Promise.all([
  fetch('https://jsonplaceholder.typicode.com/posts').then(res => res.json()),
  fetch('https://jsonplaceholder.typicode.com/users')
]);

console.log(users.length);
ABecause the second fetch response is not converted to JSON before accessing length
BBecause Promise.all does not support mixed promises and values
CBecause fetch cannot be used inside Server Components
DBecause the first fetch is missing await before then
Attempts:
2 left
💡 Hint

Check how the responses are handled before accessing properties like length.

state_output
advanced
2:00remaining
What is the state output after this React component fetches data in parallel?

Given this React functional component using useEffect and Promise.all, what will be the final state of data?

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

export default function DataFetcher() {
  const [data, setData] = useState(null);

  useEffect(() => {
    Promise.all([
      fetch('/api/a').then(res => res.json()),
      fetch('/api/b').then(res => res.json())
    ]).then(([a, b]) => {
      setData({ a, b });
    });
  }, []);

  return <pre>{JSON.stringify(data)}</pre>;
}
AAn error because useEffect cannot use async functions
B{"a": {...}, "b": {...}} (objects from both APIs)
Cnull (state never updates)
D{"a": undefined, "b": undefined}
Attempts:
2 left
💡 Hint

Consider how Promise.all and then update state inside useEffect.

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

Choose the best explanation for why parallel data fetching is recommended in Next.js Server Components.

ASequential fetching is deprecated in Next.js and will cause build errors.
BParallel fetching ensures data is fetched in a specific order required by Next.js routing.
CParallel fetching caches all data automatically, so no manual caching is needed.
DParallel fetching reduces total waiting time by starting all requests simultaneously, improving performance.
Attempts:
2 left
💡 Hint

Think about how waiting for multiple requests one after another affects load time.