0
0
NextJSframework~10 mins

Sequential data fetching in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Sequential data fetching
Start Component Render
Fetch Data 1
Wait for Data 1
Fetch Data 2 using Data 1
Wait for Data 2
Render UI with Data 1 & Data 2
User Sees Combined Data
The component starts rendering, fetches the first data, waits for it, then uses it to fetch the second data, waits again, and finally renders the UI with both data sets.
Execution Sample
NextJS
export default async function Page() {
  const res1 = await fetch('https://api.example.com/data1');
  const data1 = await res1.json();
  const res2 = await fetch(`https://api.example.com/data2/${data1.id}`);
  const data2 = await res2.json();
  return <div>{data1.name} - {data2.detail}</div>;
}
This Next.js server component fetches data1, then uses its id to fetch data2, and finally renders both.
Execution Table
StepActionEvaluationResult
1Start component renderNo data fetched yetComponent begins execution
2Fetch data1 from APIAwait fetch('https://api.example.com/data1')Response object res1 received
3Parse data1 JSONAwait res1.json(){"id": 42, "name": "Alice"}
4Fetch data2 using data1.idAwait fetch('https://api.example.com/data2/42')Response object res2 received
5Parse data2 JSONAwait res2.json(){"detail": "Likes cats"}
6Render UI with data1.name and data2.detailReturn JSX <div>Alice - Likes cats</div>UI ready to display
7Component render completeAll awaits resolvedPage shows 'Alice - Likes cats'
💡 All data fetched sequentially; component render completes with combined data.
Variable Tracker
VariableStartAfter Step 3After Step 5Final
res1undefinedResponse objectResponse objectResponse object
data1undefined{"id": 42, "name": "Alice"}{"id": 42, "name": "Alice"}{"id": 42, "name": "Alice"}
res2undefinedundefinedResponse objectResponse object
data2undefinedundefined{"detail": "Likes cats"}{"detail": "Likes cats"}
Key Moments - 2 Insights
Why do we wait for data1 before fetching data2?
Because data2's URL depends on data1.id, we must wait for data1 to finish (see execution_table step 4). Fetching data2 before data1 would cause an error.
What happens if we don't use await with fetch?
Without await, the code moves on before data arrives, so data1 and data2 would be undefined or promises, causing render errors (see variable_tracker showing undefined before awaits).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of data1?
AResponse object
Bundefined
C{"id": 42, "name": "Alice"}
DPromise
💡 Hint
Check the 'Result' column at step 3 in execution_table.
At which step does the component fetch data2?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look for the fetch action using data1.id in execution_table.
If we remove await before fetch for data1, what changes in variable_tracker?
Adata1 becomes a Promise instead of JSON object
Bdata1 becomes undefined
Cdata2 fetch happens earlier
DNo change
💡 Hint
Without await, fetch returns a Promise, not the resolved JSON (see variable_tracker start values).
Concept Snapshot
Sequential Data Fetching in Next.js Server Components:
- Use async function with await to fetch data step-by-step.
- Await first fetch, parse JSON, then use result to fetch next.
- Ensures data dependencies are respected.
- Render UI only after all data is ready.
- Avoids race conditions and undefined data errors.
Full Transcript
This visual execution trace shows how a Next.js server component fetches data sequentially. First, it starts rendering and fetches data1 from an API. It waits for data1 to arrive and parses it as JSON. Then, it uses data1's id to fetch data2 from another API endpoint, waiting again for the response and parsing it. Finally, it renders the UI combining data1 and data2. Variables like res1, data1, res2, and data2 update step-by-step, showing how data flows through the component. Key points include the necessity of awaiting each fetch to ensure data is ready before moving on, and how removing await would cause data variables to hold promises instead of actual data. The quiz questions reinforce understanding of these steps and variable states.