An operational dashboard may remain useful when one optional source fails. Use allSettled when you deliberately want independent outcomes, and make the missing part visible rather than pretending the report is complete.
Read, predict, run
The model maps settled result objects to a stable source-labelled report. The streaming page demonstrates independent waiting areas; the result policy decides whether one failed section should leave the others usable:
Before pressing Run, predict what run returns for [{"status": "fulfilled", "value": 4}, {"status": "rejected", "reason": "offline"}], from the
code above. Write your answers down first: after Run, the case table shows the
expected values beside the actual ones.
Partial Success Must Be VisibleNext.js · 8 filesimport "./globals.css";
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<header>
<strong>Review Queue · Practice lab</strong>
</header>
{children}
</body>
</html>
);
}
import { Suspense } from "react";
import Cases from "./Cases.jsx";
import Dashboard from "./Dashboard.jsx";
async function Report() {
await new Promise((resolve) => setTimeout(resolve, 600));
return <Cases />;
}
export default function Page() {
return (
<main>
<h1>Partial Success Must Be Visible</h1>
<p>app/api/dashboard/route.js loads three counts and calls run.</p>
<Dashboard />
<Suspense fallback={<p>Loading report…</p>}>
<Report />
</Suspense>
</main>
);
}
import { run, examples } from "@/lib/contract.js";
const text = (value) => JSON.stringify(value) ?? "undefined";
export default async function Cases() {
const results = await Promise.all(
examples.map(async ([input, expected]) => {
// Record the input before running, to show whether run changed it.
const before = text(input);
const actual = await run(input);
return {
input: before,
expected,
actual,
kept: text(input) === before,
pass: text(actual) === text(expected),
};
}),
);
return (
<section>
<h2>Executable cases</h2>
<p>
Checks: {results.filter((row) => row.pass).length}/{results.length}{" "}
passed
</p>
<table>
<thead>
<tr>
<th>Input</th>
<th>Expected</th>
<th>Actual</th>
<th>Input kept</th>
</tr>
</thead>
<tbody>
{results.map((row, i) => (
<tr key={i}>
<td>
<code>{row.input}</code>
</td>
<td>
<code>{text(row.expected)}</code>
</td>
<td className={row.pass ? "pass" : "fail"}>
<code>{text(row.actual)}</code>
</td>
<td className={row.kept ? "pass" : "fail"}>
{row.kept ? "yes" : "no: run changed it"}
</td>
</tr>
))}
</tbody>
</table>
</section>
);
}
"use client";
import { useState } from "react";
const labels = ["Open reviews", "Overdue reviews", "Approved today"];
export default function Dashboard() {
const [view, setView] = useState(null);
async function load(mode) {
const response = await fetch(`/api/dashboard${mode ? `?mode=${mode}` : ""}`);
const body = await response.json();
if (!response.ok) return setView({ error: `${response.status} ${body.code}` });
// run returns an array now; the exercise wraps it in an object.
setView(Array.isArray(body) ? { items: body } : body);
}
return (
<section aria-label="Dashboard">
<button onClick={() => load("")}>Load with allSettled</button>{" "}
<button onClick={() => load("all")}>Load with Promise.all</button>
{view?.error && <p role="alert">Dashboard failed: {view.error}</p>}
{view?.items && (
<ul>
{view.items.map((item, index) => (
<li key={labels[index]}>
{labels[index]}: {item.ok ? item.value : "unavailable"}
</li>
))}
</ul>
)}
{view?.complete === false && <p>Some numbers are missing.</p>}
</section>
);
}
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;
}
import { run } from "@/lib/contract.js";
import { sources } from "@/lib/sources.js";
export async function GET(request) {
const calls = () => sources.map(([, load]) => load());
if (new URL(request.url).searchParams.get("mode") === "all") {
// For comparison: one failure rejects the whole Promise.all.
try {
const values = await Promise.all(calls());
return Response.json({ values });
} catch (error) {
console.log(`dashboard failed: ${error.message}`);
return Response.json({ code: "internal_error" }, { status: 500 });
}
}
const results = await Promise.allSettled(calls());
results.forEach((result, index) => {
if (result.status === "rejected")
console.log(`${sources[index][0]} failed: ${result.reason.message}`);
});
return Response.json(run(results));
}
export function run(results) {
return results.map((result) =>
result.status === "fulfilled"
? { ok: true, value: result.value }
: { ok: false, value: null },
);
}
export const examples = [
[
[
{ status: "fulfilled", value: 4 },
{ status: "rejected", reason: "offline" },
],
[
{ ok: true, value: 4 },
{ ok: false, value: null },
],
],
];
// Three independent counts for the reviewers' dashboard. The overdue service
// is down today.
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
export const sources = [
["Open reviews", async () => (await wait(100), 4)],
["Overdue reviews", async () => { await wait(150); throw new Error("overdue service offline"); }],
["Approved today", async () => (await wait(200), 7)],
];
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
The case table computes these results on the server side of the simulation.
It compares values; it does not prove a production deployment or database works.
The failure to avoid
Replacing a failed metric with zero makes an outage look like good performance. Dropping failed sources entirely hides the incompleteness from readers.
Compare Promise.all with Promise.allSettled for three sources where the
second fails. Promise.all rejects as soon as it does, and the whole dashboard
shows an error though two sources answered. allSettled waits for all three
and reports each outcome, so the page shows two numbers and an honest gap.
See it in a route handler
The lab's app/api/dashboard/route.js loads three counts for the reviewers'
dashboard from lib/sources.js. Today the overdue service is down, so its
loader throws. The handler collects all three outcomes and passes them to
run:
The same route with ?mode=all uses Promise.all instead, for comparison.
Predict what the dashboard shows for each button, then press them.
A dashboard with one source downNext.js · 8 filesimport "./globals.css";
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<header>
<strong>Review Queue · Practice lab</strong>
</header>
{children}
</body>
</html>
);
}
import { Suspense } from "react";
import Cases from "./Cases.jsx";
import Dashboard from "./Dashboard.jsx";
async function Report() {
await new Promise((resolve) => setTimeout(resolve, 600));
return <Cases />;
}
export default function Page() {
return (
<main>
<h1>Partial Success Must Be Visible</h1>
<p>app/api/dashboard/route.js loads three counts and calls run.</p>
<Dashboard />
<Suspense fallback={<p>Loading report…</p>}>
<Report />
</Suspense>
</main>
);
}
import { run, examples } from "@/lib/contract.js";
const text = (value) => JSON.stringify(value) ?? "undefined";
export default async function Cases() {
const results = await Promise.all(
examples.map(async ([input, expected]) => {
// Record the input before running, to show whether run changed it.
const before = text(input);
const actual = await run(input);
return {
input: before,
expected,
actual,
kept: text(input) === before,
pass: text(actual) === text(expected),
};
}),
);
return (
<section>
<h2>Executable cases</h2>
<p>
Checks: {results.filter((row) => row.pass).length}/{results.length}{" "}
passed
</p>
<table>
<thead>
<tr>
<th>Input</th>
<th>Expected</th>
<th>Actual</th>
<th>Input kept</th>
</tr>
</thead>
<tbody>
{results.map((row, i) => (
<tr key={i}>
<td>
<code>{row.input}</code>
</td>
<td>
<code>{text(row.expected)}</code>
</td>
<td className={row.pass ? "pass" : "fail"}>
<code>{text(row.actual)}</code>
</td>
<td className={row.kept ? "pass" : "fail"}>
{row.kept ? "yes" : "no: run changed it"}
</td>
</tr>
))}
</tbody>
</table>
</section>
);
}
"use client";
import { useState } from "react";
const labels = ["Open reviews", "Overdue reviews", "Approved today"];
export default function Dashboard() {
const [view, setView] = useState(null);
async function load(mode) {
const response = await fetch(`/api/dashboard${mode ? `?mode=${mode}` : ""}`);
const body = await response.json();
if (!response.ok) return setView({ error: `${response.status} ${body.code}` });
// run returns an array now; the exercise wraps it in an object.
setView(Array.isArray(body) ? { items: body } : body);
}
return (
<section aria-label="Dashboard">
<button onClick={() => load("")}>Load with allSettled</button>{" "}
<button onClick={() => load("all")}>Load with Promise.all</button>
{view?.error && <p role="alert">Dashboard failed: {view.error}</p>}
{view?.items && (
<ul>
{view.items.map((item, index) => (
<li key={labels[index]}>
{labels[index]}: {item.ok ? item.value : "unavailable"}
</li>
))}
</ul>
)}
{view?.complete === false && <p>Some numbers are missing.</p>}
</section>
);
}
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;
}
import { run } from "@/lib/contract.js";
import { sources } from "@/lib/sources.js";
export async function GET(request) {
const calls = () => sources.map(([, load]) => load());
if (new URL(request.url).searchParams.get("mode") === "all") {
// For comparison: one failure rejects the whole Promise.all.
try {
const values = await Promise.all(calls());
return Response.json({ values });
} catch (error) {
console.log(`dashboard failed: ${error.message}`);
return Response.json({ code: "internal_error" }, { status: 500 });
}
}
const results = await Promise.allSettled(calls());
results.forEach((result, index) => {
if (result.status === "rejected")
console.log(`${sources[index][0]} failed: ${result.reason.message}`);
});
return Response.json(run(results));
}
export function run(results) {
return results.map((result) =>
result.status === "fulfilled"
? { ok: true, value: result.value }
: { ok: false, value: null },
);
}
export const examples = [
[
[
{ status: "fulfilled", value: 4 },
{ status: "rejected", reason: "offline" },
],
[
{ ok: true, value: 4 },
{ ok: false, value: null },
],
],
];
// Three independent counts for the reviewers' dashboard. The overdue service
// is down today.
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
export const sources = [
["Open reviews", async () => (await wait(100), 4)],
["Overdue reviews", async () => { await wait(150); throw new Error("overdue service offline"); }],
["Approved today", async () => (await wait(200), 7)],
];
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Check your prediction
Load with allSettled shows Open reviews: 4, Overdue reviews:
unavailable and Approved today: 7, and the server log names the failed
source. Load with Promise.all shows Dashboard failed: 500
internal_error: one source's outage hid the two numbers that did arrive.
Notice what's still missing: nothing in the response says the report is
incomplete, so a caller has to inspect every item to find out. The exercise
adds that.
Exercise
Extend the contract
Add complete as a top-level boolean next to items. It is true only when every source succeeded.
Edit lib/contract.js, then rerun the original cases. If the requested rule
changes an earlier result, update that expected value deliberately. Keep the
other cases. Add the acceptance cases to examples so the preview checks them too.
items.every((item) => item.ok) computes complete from the same list the page
renders, so the two can't disagree. An empty list counts as complete, which is
right: nothing is missing. Wrapping the array in an object breaks callers that
expected an array, so in a public API it's a versioned change.
Explain it without the code
Write how the dashboard should look when one of three sources failed, and what
it must never show in the failed slot.
Going further
Read the Next.js guide for the framework boundary around this rule.
The preview runs code-playpad's Next.js simulation. Real caching, hydration,
authentication and persistence require the real-server checks described on Day 7.
Completion is saved in this browser only. It does not measure mastery.