completefrontend

Predict the outputEasy

A teammate rewrites two awaits as one Promise.all. In what order do the log lines print, before and after?

The tea page reads the tea, which takes 400 ms, and its reviews, which take 200 ms, and each read logs when it starts and when it's done. /before is the page as it was; /after is the teammate's version. Both sit under app/loading.tsx. You build the app, start it, and ask for /before, then /after. What does the server's terminal print?

app/layout.tsx
export const metadata = { title: "Leaf and Kettle" };
 
export default function RootLayout({ children }: LayoutProps<"/">) {
  return (
    <html lang="en">
      <body>{children}</body>
    </html>
  );
}
app/loading.tsx
export default function Loading() {
  return <p>Brewing…</p>;
}
lib/shop.ts
import { connection } from "next/server";
 
const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
 
// Read for each request, never while building. The tea takes 400 ms, the reviews 200 ms.
export async function getTea() {
  await connection();
  console.log("tea: start");
  await wait(400);
  console.log("tea: done");
  return { name: "Oolong tea", about: "Rich and smooth." };
}
 
export async function getReviews() {
  await connection();
  console.log("reviews: start");
  await wait(200);
  console.log("reviews: done");
  return ["Kenji: My Sunday pot.", "Ana: Lovely with lunch."];
}
app/before/page.tsx
import { getReviews, getTea } from "@/lib/shop";
 
export default async function Before() {
  const tea = await getTea();
  const reviews = await getReviews();
  console.log("page: ready");
 
  return (
    <main>
      <h1>{tea.name}</h1>
      <p>{tea.about}</p>
      <ul>
        {reviews.map((review) => (
          <li key={review}>{review}</li>
        ))}
      </ul>
    </main>
  );
}
app/after/page.tsx
import { getReviews, getTea } from "@/lib/shop";
 
export default async function After() {
  const [tea, reviews] = await Promise.all([getTea(), getReviews()]);
  console.log("page: ready");
 
  return (
    <main>
      <h1>{tea.name}</h1>
      <p>{tea.about}</p>
      <ul>
        {reviews.map((review) => (
          <li key={review}>{review}</li>
        ))}
      </ul>
    </main>
  );
}

It's a project create-next-app made (Next.js 16.4.3, TypeScript, the App Router, and Cache Components on) with these files, built with next build and run with next start. What happens:

  1. You run next build, then next start.
  2. You ask the server for /before.
  3. Then you ask it for /after.

The answer

Work out what Next.js does at each step: what the build prints, what each request answers, and what the page shows. Then check.

Checking your sign-in…