TypeScript traps · Chapter 15 of 16
A Promise<void> nobody awaited
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Objects
- 3 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of TypeScript traps are free. This one comes with the all labs plan.
Why you need this
A function type that returns void does not mean "returns nothing". It means "the caller will ignore whatever comes back". So any function fits, including one that returns a number, or a promise:
type Job = () => void;
let counted = 0;
const countStock: Job = async () => {
await Promise.resolve();
counted = 42;
};
const jobs: Job[] = [countStock, () => console.log('stock counted:', counted)];
for (const job of jobs) job();
typecheck says No type errors., and node prints:
stock counted: 0
That rule is on purpose: it lets you pass (n) => names.push(n) to forEach, even though push returns a number. But the promise an async job returns is the only way to know when it has finished, and whether it failed. Typed as void, it is a promise nobody is expected to await: a floating promise. The loop calls countStock, which stops at its first await, and the next job runs straight away.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.ts: report ₹0 on purpose
Write reproduce() in repro.ts. Build a fake bank, an object whose settleUpi() resolves to 18240 (the day's UPI, in rupees), and two closing steps, in this order: an async settle step that awaits bank.settleUpi() and keeps the amount, and a report step that writes the report text 'UPI settled: ₹<the amount kept so far>'. Run the steps with closeDay from closing.ts, await it, and resolve to the report text.
The check runs your reproduce() twice, each time in a separate node: beside the original closing.ts it must resolve to 'UPI settled: ₹0' (the report from the ticket), and beside a fixed closing.ts it must resolve to 'UPI settled: ₹18240'. So the settle step must really await the bank. No real waiting: the fake bank answers straight away, and reproduce() must finish within 2 seconds.
- Task 2
closing.ts: wait for each closing step
Fix closeDay(steps) in closing.ts so the steps really run one after another: each step finishes before the next one starts, whether it is a plain function or an async one. closeDay resolves only after the last step has finished, and then logs day closed. When a step fails, by throwing or by returning a promise that rejects, closeDay rejects with that same error, the later steps do not run, and day closed is not logged. A list of plain steps, or no steps at all, still works.
Also change the Step type so it says the truth: a step may return a Promise<void>. The check holds each step's answer back with its own steps and looks at what has started, and also type-checks closing.ts: a Step's return type must include Promise<void> and must not be any. Running every step at once with Promise.all is not a fix: the report would still run before the settlement has finished.
- Task 3
closing.test.ts: a test that would have caught it
Write a vitest test file, closing.test.ts, that would have caught this bug before the first ₹0 report. The check runs your file three times: beside the original closing.ts, where at least one test must fail; beside the fixed closing.ts, where every test must pass; and beside a wrong fix that starts every step at once with Promise.all, where at least one test must fail.
So hold the settle step's answer back with a promise you settle yourself, as in case 14, and check the report step has not run yet; then let the settle step finish and check the report step ran and closeDay resolved. A rejection nobody handles does not fail a vitest test, so check everything with expect. No real waiting: each test must finish within 3 seconds.
What you will learn
- repro.ts: report ₹0 on purpose
- closing.ts: wait for each closing step
- closing.test.ts: a test that would have caught it
More in Objects
Build "A Promise<void> nobody awaited" yourself
Tapri moved to TypeScript, and Kabir, its senior developer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of TypeScript traps are free. This one comes with the all labs plan.