Consider this Next.js component that fetches user and post data in parallel using Promise.all. What will it render?
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> ); }
Think about what Promise.all returns and how the data is destructured.
The component uses Promise.all to fetch user and posts simultaneously. Both promises resolve correctly, so user.name is 'Alice' and posts is an array with two items. The component renders the user name and the list of posts.
Choose the code snippet that correctly fetches data1 and data2 in parallel inside a Next.js server component.
Remember that Promise.all runs promises in parallel and waits for all to finish.
Option A uses Promise.all to run both fetches in parallel and waits for both to resolve. Option A runs fetches sequentially. Option A starts fetches but awaits them separately, losing parallelism. Option A does not await the promises properly.
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?
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> ); }
Check what fetch(...).then(...) returns and how it is used in JSX.
fetch(...).then(...) returns a promise. The component tries to access .value on the promise instead of the resolved data, causing a runtime error.
Consider this React functional component that fetches two APIs in parallel using useEffect and Promise.all. What will it display after data loads?
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> ); }
Think about how useEffect and state updates work asynchronously.
The component shows 'Loading...' while data is null. After both fetches complete, state updates with user and posts data, causing a re-render that displays the user name and posts list.
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.
Think about how waiting for multiple tasks works in real life, like waiting in line versus doing tasks at the same time.
Parallel fetching sends multiple requests at once, so the total time is roughly the longest single request time, not the sum. This speeds up page rendering and improves user experience.