Performance: Sequential data fetching
This affects page load speed and interaction readiness by delaying data availability until each fetch completes in order.
Jump into concepts and practice - no test required
const [data1, data2, data3] = await Promise.all([ fetch('/api/data1').then(res => res.json()), fetch('/api/data2').then(res => res.json()), fetch('/api/data3').then(res => res.json()) ]);
const data1 = await fetch('/api/data1').then(res => res.json()); const data2 = await fetch('/api/data2').then(res => res.json()); const data3 = await fetch('/api/data3').then(res => res.json());
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Sequential fetches | Minimal DOM nodes until data arrives | Single reflow after all data loads | High paint delay due to waiting | [X] Bad |
| Parallel fetches | Minimal DOM nodes until data arrives | Single reflow after all data loads | Lower paint delay, faster LCP | [OK] Good |
async function fetchData() {
const res1 = await fetch('https://api.example.com/user');
const user = await res1.json();
const res2 = await fetch(`https://api.example.com/posts?userId=${user.id}`);
const posts = await res2.json();
return posts.length;
}async function getData() {
const user = fetch('/api/user');
const posts = await fetch(`/api/posts?userId=${user.id}`);
return posts.json();
}