0
0
NextJSframework~10 mins

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

Choose your learning style9 modes available
Concept Flow - Parallel data fetching
Start Component Render
Trigger Data Fetches
Run fetch1 and fetch2 in parallel
Wait for both fetches to complete
Combine results
Render UI with data
User sees combined data
The component starts rendering, triggers multiple data fetches at the same time, waits for all to finish, then renders the UI with combined data.
Execution Sample
NextJS
async function fetchData() {
  const [res1, res2] = await Promise.all([
    fetch('/api/data1'),
    fetch('/api/data2')
  ]);
  const data1 = await res1.json();
  const data2 = await res2.json();
  return { data1, data2 };
}
This code fetches two API endpoints at the same time, waits for both to finish, then returns their JSON data together.
Execution Table
StepActionEvaluationResult
1Start fetchData functionNo data fetched yetFunction begins
2Call Promise.all with fetch('/api/data1') and fetch('/api/data2')Both fetches start simultaneouslyTwo pending promises
3Wait for both fetches to resolvefetch('/api/data1') resolves firstres1 contains response 1, res2 pending
4fetch('/api/data2') resolvesBoth fetches doneres2 contains response 2
5Call res1.json()Parse JSON from response 1data1 contains parsed data 1
6Call res2.json()Parse JSON from response 2data2 contains parsed data 2
7Return combined dataReturn { data1, data2 }Function returns combined data object
8Component receives dataRender UI with data1 and data2UI shows combined data
💡 Both fetches complete and JSON parsed, data combined and returned
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5After Step 6Final
res1undefinedResponse object from /api/data1 (resolved)Response object from /api/data1Response object from /api/data1Response object from /api/data1Response object from /api/data1
res2undefinedPendingResponse object from /api/data2 (resolved)Response object from /api/data2Response object from /api/data2Response object from /api/data2
data1undefinedundefinedundefinedParsed JSON data from res1Parsed JSON data from res1Parsed JSON data from res1
data2undefinedundefinedundefinedundefinedParsed JSON data from res2Parsed JSON data from res2
Key Moments - 3 Insights
Why do both fetches start at the same time instead of one after the other?
Because Promise.all runs both fetch calls simultaneously, as shown in execution_table step 2, so they don't wait for each other.
What happens if one fetch fails while the other succeeds?
Promise.all rejects immediately if any fetch fails, so the combined result won't return. This is why error handling is important but not shown here.
Why do we await res1.json() and res2.json() separately after Promise.all?
Because Promise.all only waits for the fetch responses, not the JSON parsing. Parsing is asynchronous and must be awaited separately as in steps 5 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the state of res2?
Ares2 is undefined
Bres2 has resolved with response data
Cres2 is still pending (not resolved)
Dres2 contains parsed JSON data
💡 Hint
Check the 'Evaluation' and 'Result' columns at step 3 in execution_table
At which step does the function return the combined data object?
AStep 7
BStep 5
CStep 3
DStep 8
💡 Hint
Look for the 'Return combined data' action in execution_table
If we remove Promise.all and await fetches one by one, how would the execution_table change?
AFetches would run in parallel as before
BFetch('/api/data2') would start only after fetch('/api/data1') completes
CBoth fetches would fail
DThe function would return before any fetch completes
💡 Hint
Promise.all runs fetches together; removing it causes sequential fetches
Concept Snapshot
Parallel data fetching in Next.js:
Use Promise.all to start multiple fetches at once.
Await Promise.all to wait for all to finish.
Then parse each response's JSON separately.
This speeds up data loading by running requests together.
Full Transcript
This visual execution shows how parallel data fetching works in Next.js using Promise.all. The component triggers two fetch calls at the same time. The execution table traces each step: starting fetches, waiting for both to resolve, parsing JSON from each response, and finally returning combined data. The variable tracker shows how response and data variables change after each step. Key moments clarify why fetches run simultaneously, how errors affect Promise.all, and why JSON parsing is awaited separately. The quiz tests understanding of fetch states and the effect of Promise.all. The snapshot summarizes the pattern: use Promise.all to fetch in parallel, await all, then parse JSON. This approach improves performance by not waiting for each fetch sequentially.