CodePark/ questions / Data fetching & caching
START PRACTICINGPRACTICE
Frontend question · Next.js › Data fetching & caching

getUser and getPosts each take about 1 second and are independent. Which version loads faster?

// Version A
const user = await getUser(id);
const posts = await getPosts(id);

// Version B
const userPromise = getUser(id);
const postsPromise = getPosts(id);
const [user, posts] = await Promise.all([userPromise, postsPromise]);
Version B, because both requests start before either is awaited

Version B is correct. It starts both requests immediately and then waits, so the total is about 1 second. Version A is a waterfall: getPosts starts only after getUser finishes, so the total is about 2 seconds. The tempting "They are the same, because Next.js parallelizes awaits automatically" is wrong. Next.js does not reorder your awaits. Each await pauses the function until that Promise finishes.