An await pauses the function until that one promise settles. Three awaits
in a row therefore run one after another, even when the second never needed the
first's answer. That's a waterfall: the page waits for the sum of the
delays instead of the longest one.
Read, predict, run
The page loads three things from lib/library.js. Each pretend call has a fixed
delay: getReader 300 ms, getItems 400 ms, and getProgress 200 ms.
getProgress needs the reader's id; getItems needs nothing.
JSX
export default async function Page() { const started = Date.now(); const reader = await getReader(); const items = await getItems(); const progress = await getProgress(reader.id); const elapsed = Date.now() - started; // …renders the reader, the items and "Loaded in {elapsed} ms"}
Before running, predict roughly what Loaded in … ms will say.
Start Independent Work TogetherNext.js · 4 filesimport "./globals.css";
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<header>
<strong>Study Library · Practice lab</strong>
</header>
{children}
</body>
</html>
);
}
import { getReader, getItems, getProgress } from "@/lib/library.js";
export const dynamic = "force-dynamic";
export default async function Page() {
const started = Date.now();
const reader = await getReader();
const items = await getItems();
const progress = await getProgress(reader.id);
const elapsed = Date.now() - started;
console.log(`Loaded in ${elapsed} ms`);
return (
<main>
<h1>Start Independent Work Together</h1>
<p>
{reader.name} has finished {progress.done} of {items.length} items.
</p>
<p role="status">Loaded in {elapsed} ms</p>
</main>
);
}
body {
font: 16px/1.5 system-ui;
margin: 0;
color: #182b3c;
background: #f4f8fc;
}
header,
main {
max-width: 900px;
margin: auto;
padding: 20px;
}
header {
border-bottom: 1px solid #cbd5e1;
}
a {
color: #075985;
}
button,
input,
textarea,
select {
font: inherit;
padding: 8px;
margin: 4px;
}
button {
cursor: pointer;
}
table {
border-collapse: collapse;
width: 100%;
}
th,
td {
padding: 10px;
border: 1px solid #cbd5e1;
text-align: left;
vertical-align: top;
}
pre,
code {
white-space: pre-wrap;
overflow-wrap: anywhere;
}
.pass {
color: #166534;
}
.fail,
[role="alert"] {
color: #a01818;
}
section {
margin: 16px 0;
padding: 12px;
background: white;
border-radius: 8px;
}
nav {
display: flex;
gap: 16px;
}
// Pretend database calls, each with a fixed delay so timings are predictable.
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
export async function getReader() {
await wait(300);
return { id: "ada", name: "Ada" };
}
export async function getItems() {
await wait(400);
return ["Routing", "Forms", "Caching"];
}
// Needs the reader's id, so it can only start once getReader has finished.
export async function getProgress(readerId) {
await wait(200);
return { readerId, done: 1 };
}
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Check your prediction
About 900 ms, a few milliseconds more on a busy machine:
300 + 400 + 200. Trace the timeline:
Text
0 ms getReader starts300 ms getReader done, getItems starts700 ms getItems done, getProgress starts900 ms getProgress done
getItems sat idle for 300 ms waiting for a reader it never uses.
The failure to avoid
The fix is not "wrap everything in Promise.all". getProgress really does
need reader.id, so it can't start before getReader finishes. Start only the
independent work together and keep the dependent step after the thing it
depends on.
Promise.all has its own rule to remember: if one promise rejects, the whole
Promise.all rejects at once, but the other calls keep running; it doesn't
cancel them. Whether one failure should fail the whole page is a decision, not
an accident of how you wrote the awaits.
Exercise
Remove the waterfall
Change the page so getReader and getItems run at the same time, while
getProgress still receives the reader's id. Loaded in … ms should drop to
about 600 ms, and the page must still say Ada has finished 1 of 3 items.
Your working spaceNext.js · 4 filesimport "./globals.css";
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<header>
<strong>Study Library · Practice lab</strong>
</header>
{children}
</body>
</html>
);
}
import { getReader, getItems, getProgress } from "@/lib/library.js";
export const dynamic = "force-dynamic";
export default async function Page() {
const started = Date.now();
const reader = await getReader();
const items = await getItems();
const progress = await getProgress(reader.id);
const elapsed = Date.now() - started;
console.log(`Loaded in ${elapsed} ms`);
return (
<main>
<h1>Start Independent Work Together</h1>
<p>
{reader.name} has finished {progress.done} of {items.length} items.
</p>
<p role="status">Loaded in {elapsed} ms</p>
</main>
);
}
body {
font: 16px/1.5 system-ui;
margin: 0;
color: #182b3c;
background: #f4f8fc;
}
header,
main {
max-width: 900px;
margin: auto;
padding: 20px;
}
header {
border-bottom: 1px solid #cbd5e1;
}
a {
color: #075985;
}
button,
input,
textarea,
select {
font: inherit;
padding: 8px;
margin: 4px;
}
button {
cursor: pointer;
}
table {
border-collapse: collapse;
width: 100%;
}
th,
td {
padding: 10px;
border: 1px solid #cbd5e1;
text-align: left;
vertical-align: top;
}
pre,
code {
white-space: pre-wrap;
overflow-wrap: anywhere;
}
.pass {
color: #166534;
}
.fail,
[role="alert"] {
color: #a01818;
}
section {
margin: 16px 0;
padding: 12px;
background: white;
border-radius: 8px;
}
nav {
display: flex;
gap: 16px;
}
// Pretend database calls, each with a fixed delay so timings are predictable.
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
export async function getReader() {
await wait(300);
return { id: "ada", name: "Ada" };
}
export async function getItems() {
await wait(400);
return ["Routing", "Forms", "Caching"];
}
// Needs the reader's id, so it can only start once getReader has finished.
export async function getProgress(readerId) {
await wait(200);
return { readerId, done: 1 };
}
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea
A call starts when you call the function, not when you await its promise.
To run two calls together, start both before waiting for either.
Hint 2: a first step
Promise.all([a, b]) waits for both and gives back [resultA, resultB] in the
same order. Use it for the two calls that don't depend on anything, and leave
the third line alone. Then read the new timing.
Both calls start in the same line, so the page waits for the slower of the two,
400 ms, then 200 ms for the progress that needed the reader: about 600 ms.
Putting getProgress(reader.id) inside the Promise.all too is impossible:
reader doesn't exist yet.
Going further: the last 100 ms
The solution still makes getProgress wait for getItems, which it doesn't
need. Start getItems first, keep its promise, and wait for it only at the end:
Now getProgress starts at 300 ms, alongside the still-running getItems, and
everything is done at about 500 ms. It's harder to read, so reach for it only
when the time matters. On a real page, a <Suspense> boundary (the next
lesson) is often the better tool: it lets the fast parts show while the slow
ones are still loading.
Explain it without the code
A page awaits a user, then that user's orders, then a list of shipping
countries. Which of the three can start at the same time, and which must wait?
Write the order you'd start them in.
Completion is saved in this browser only. It does not measure mastery.