Consider this Next.js component that fetches two APIs in parallel using Promise.all. What will it render?
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> ); }
Think about how Promise.all helps fetch data simultaneously and what the API returns.
The component fetches posts and users in parallel, waits for both, then parses JSON. The posts API returns 100 posts, and the users API returns a list where the first user's name is 'Leanne Graham'. So the rendered output shows these values.
Choose the code snippet that correctly fetches two APIs in parallel inside a Next.js Server Component.
Look for the option that starts both fetches at the same time and waits for both to finish.
Option A uses Promise.all to start both fetches simultaneously and waits for both to complete, which is the correct parallel fetching pattern.
Examine this code snippet inside a Next.js Server Component. Why does it cause a runtime error?
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);
Check how the responses are handled before accessing properties like length.
The second fetch returns a Response object, not JSON data. Accessing users.length causes an error because users is not an array but a Response. The fix is to call res.json() on the second fetch as well.
Given this React functional component using useEffect and Promise.all, what will be the final state of data?
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>; }
Consider how Promise.all and then update state inside useEffect.
The component fetches both APIs in parallel, waits for both JSON responses, then sets the state with both results. The state data becomes an object with keys a and b holding the fetched data.
Choose the best explanation for why parallel data fetching is recommended in Next.js Server Components.
Think about how waiting for multiple requests one after another affects load time.
Parallel fetching starts all requests at once, so the total wait time is roughly the longest single request, not the sum of all. This improves page load speed and user experience.