Giving each attempt a fresh timeout can let a multi-step request run far longer than intended. Carry one deadline through the operation and calculate the remaining budget at each boundary.
Read, predict, run
The function takes explicit clock values so tests can cover the exact deadline. It returns zero when the budget is exhausted. An actual fetch needs an AbortSignal and cleanup; arithmetic alone does not cancel work:
JavaScript
export function run({ deadline, now }) { return Math.max(0, deadline - now);}
Before pressing Run, predict what run returns for {"deadline": 100, "now": 40} and {"deadline": 100, "now": 110}, from the
code above. Write your answers down first: after Run, the case table shows the
expected values beside the actual ones.
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Check your prediction
Input {"deadline": 100, "now": 40} → 60.
Input {"deadline": 100, "now": 110} → 0.
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
A timeout that only rejects the caller’s promise can leave underlying work running. Cancellation is cooperative and must be passed to the dependency that performs the work.
Compare a fresh 100 ms timeout per call with one deadline for the operation.
Three sequential calls, each slow: per-call timeouts let the request run
300 ms. With deadline carried through, the second call gets only what's left
after the first, and the third may get 0 and not start at all.
Exercise
Extend the contract
Cap the budget for one downstream call at 30, even when the whole operation has more time left.
Find the failure first: with deadline: 1000 and now: 0, what does the
current run give one call, and what goes wrong if that call hangs? Add a case
for it, then fix.
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.
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea
Two limits apply to every call: the time the whole operation has left, and the
most any single dependency should get. The call gets whichever is smaller.
Hint 2: a first step
Take the smaller of the remaining total budget and the per-call cap.
Show one solution
Replace the exported run function and any constants shown here; keep the
examples export underneath it. Compare the result with your own approach.
JavaScript
export function run({ deadline, now }) { return Math.min(30, Math.max(0, deadline - now));}
Math.max(0, deadline - now) is the total time left, never negative.
Math.min(30, …) caps it per call, so one slow dependency can't use the whole
budget and starve the next. When less than 30 remains, the remaining time wins.
Explain it without the code
Write the budgets three sequential calls get with deadline: 70 from now: 0,
if each call uses its whole budget.
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.