TypeScript for real code · Chapter 26 of 36
Waiting with types
By the end of this chapter you can type async functions and the promises they return, keep a value's type through Promise.race and Promise.all, and spot a missing await before it ships. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Real-world TypeScript
- 4 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of TypeScript for real code are free. This one comes with the all labs plan.
Why you need this
Ravi opens the morning dashboard on a slow monsoon network and waits. Kabir reads the code: three requests, one after another, and every result typed any. Bunty's order total, written last week, crashes with TypeError: orders.find is not a function. And the rider app hangs forever when the server does not answer.
With any, TypeScript cannot tell a promise from the value it will hold, so a missing await compiles. And the value is not there yet, so you cannot even print it to see what it is: you get Promise { <pending> }.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
fetchFrom: each path gives a promise of its own data
fetchFrom(path) answers after 20 ms with that path's data from SERVER. Make it generic over the path, with a type parameter P that extends keyof Routes, and return Promise<Routes[P]> instead of Promise<any>.
Then await fetchFrom('/menu') is a MenuItem[] (the prices are 40, 25 and 30), await fetchFrom('/riders') is a Rider[] (the first is Kiran), and fetchFrom('/menuu') is a type error. A promise is not its value: reading .length on fetchFrom('/orders') without await is a type error too.
- Task 2
totalFor: the missing await and the real return type
totalFor(orderId) resolves to the total of that order: totalFor('o1') to 105, totalFor('o2') to 30, and an order that does not exist, like 'o99', to 0. It returns a promise, and its return type must be exactly Promise<number>: an async function cannot return a plain number.
Bunty forgot an await, so orders is the promise and not the list, and the app crashes with orders.find is not a function. Fix both.
- Task 3
withTimeout: a time limit that keeps the type
withTimeout(promise, ms) resolves like promise if it is fast enough, and rejects with new Error('timed out after <ms> ms') if it takes longer than ms milliseconds: a 50 ms limit on a 500 ms promise rejects with 'timed out after 50 ms', and a 10 ms promise for 'chai' resolves to 'chai'.
It must keep the promise's type: withTimeout(fetchFrom('/menu'), 100) is a Promise<MenuItem[]>, and withTimeout(Promise.resolve(40), 100) is a Promise<number>. Make it generic, with no any left, and the time limit must be a number, so '100' is a type error. Use Promise.race with a promise that only ever rejects.
- Task 4 · interview question
Interview: loadDashboard, everything at once
The morning dashboard needs the menu, the riders and the orders. Loading them one after another takes 60 ms; load all three at once with Promise.all, so the dashboard is ready after 20 ms. It resolves to an object with menu (3 items), riders (2) and orders (2).
Remove the Promise<any>, so that Dashboard, which is read from loadDashboard's return type, is exactly { menu: MenuItem[]; riders: Rider[]; orders: Order[] }. Dev will ask why the awaits one after another are slow, and how Promise.all keeps each part's type.
What you will learn
- Promise types: an async function returns Promise<T>, await turns it into T, and Awaited<T> names that type.
- A promise per key: a generic async function whose answer's type follows its argument.
- Racing and waiting together: Promise.race with a Promise<never>, and Promise.all, which runs promises at once and keeps each type.
More in Real-world TypeScript
Build "Waiting with types" yourself
Tapri’s codebase grows, and types keep it honest. 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 for real code are free. This one comes with the all labs plan.