Course map
Stage 1: App Router Foundations
Stage 2: The Rendering Model
Stage 3: Data, Caching and Mutations
Stage 4: Full-Stack Next.js
Stage 5: Production and Specialization

Part 05 / Topic 2 of 6

async Server Components: await your data directly

Because a Server Component only ever runs on the server, it can do something a client-side React component never could: be an async function, and simply await its own data before returning markup. Apna Bazaar's owner wants an internal "wholesale desk" page — the cost price and margin behind every product, numbers no customer should ever see — and it is built exactly this way, with a small data-layer module standing in for a real database:

// harness/app/store/lib/wholesale.ts
import "server-only";
import { products } from "./products";

export type WholesaleLine = {
  id: string;
  name: string;
  retailPrice: number;
  costPrice: number;
};

const costRatioById: Record<string, number> = { p1: 0.55, p2: 0.62, p3: 0.58 };

export const SUPPLIER_HELPLINE = "+91-98xxxxxx01";

export async function getWholesaleLines(): Promise<WholesaleLine[]> {
  await Promise.resolve();
  return products.map((product) => ({
    id: product.id,
    name: product.name,
    retailPrice: product.price,
    costPrice: Math.round(product.price * costRatioById[product.id]),
  }));
}
// harness/app/store/(shop)/wholesale/page.tsx
import Link from "next/link";
import { getWholesaleLines, SUPPLIER_HELPLINE } from "../../lib/wholesale";

export default async function WholesalePage() {
  const lines = await getWholesaleLines();
  // ...render a table of lines...
}

WholesalePage is declared async, and the component body awaits getWholesaleLines() before it ever returns JSX — no useEffect fetching after the first render, no isLoading state, no data ?? null guard. The component simply waits, the same way an async function anywhere else in Node would wait on a real database driver's query() call. The real response proves the table rendered with the resolved data already in place, every time, because by the time any HTML reaches a browser the await has already finished on the server:

$ curl -s http://localhost:PORT/store/wholesale | grep -o '<main[^>]*>.*</main>'
<main style="font-family:system-ui;padding:2rem;max-width:560px"><h1>Wholesale desk</h1><p>Internal margin view — never shown to a customer browsing Apna Bazaar.</p><table style="width:100%;border-collapse:collapse"><thead><tr><th style="text-align:left">Product</th><th style="text-align:right">Retail</th><th style="text-align:right">Cost</th><th style="text-align:right">Margin</th></tr></thead><tbody><tr><td>Jaipur Blue Pottery Vase</td><td style="text-align:right">₹<!-- -->1,299</td><td style="text-align:right">₹<!-- -->714</td><td style="text-align:right">₹<!-- -->585</td></tr><tr><td>Banarasi Silk Saree</td><td style="text-align:right">₹<!-- -->4,999</td><td style="text-align:right">₹<!-- -->3,099</td><td style="text-align:right">₹<!-- -->1,900</td></tr><tr><td>Chennai Filter Coffee Set</td><td style="text-align:right">₹<!-- -->899</td><td style="text-align:right">₹<!-- -->521</td><td style="text-align:right">₹<!-- -->378</td></tr></tbody></table><p>Supplier helpline (staff only): <!-- -->+91-98xxxxxx01</p><p><a href="/store">Back to Apna Bazaar</a></p></main>

🔍 Going deeper — a genuinely new finding beyond TOOLING.md's new Date() check: TOOLING.md already found that cacheComponents: true (on by default in this harness, since Part 1) rejects a Server Component that reads a non-deterministic value like new Date() during prerendering. This part found something sharper while writing getWholesaleLines(): the build rejects a bare timer, even one whose resolved value never changes and is never read. Swap the harmless await Promise.resolve(); above for await new Promise((resolve) => setTimeout(resolve, 10)); — delaying, but still always resolving to the same undefined — and the real build fails, for real, with exactly the same error TOOLING.md recorded for new Date():

$ npm run build
...
Error: Route "/store/wholesale": Next.js encountered uncached or runtime data during prerendering.

`fetch(...)`, `cookies()`, `headers()`, `params`, `searchParams`, or `connection()` accessed outside of `<Suspense>` prevents the route from being prerendered, blocking the page load and leading to a slower user experience.

So the rule isn't just "could this value change between two prerenders" — it's "could this await itself take an unpredictable amount of time," which a setTimeout-backed Promise always can, even resolving to a constant. Reverting to await Promise.resolve(); (or no await worth flagging at all) builds clean again. <Suspense> (Part 7) and "use cache" (Stage 3) are the two real fixes for a genuinely slow await; neither is taught yet, so this page's data stays instant on purpose.

Full files: examples/01-wholesale-data-module.ts, examples/02-wholesale-page.tsx.

Done here? The practice set for Part 5 — including the solutions — is on the part page.

Search the guide