Async functions and promises¶
This lesson covers Promise types, async/await, typed error handling, and the risks of any at API boundaries.
Promise types¶
A Promise<T> resolves to a value of type T.
function wait(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms))
}
async functions¶
An async function always returns a Promise.
async function fetchUser(id: string): Promise<User> {
const response = await fetch(`/api/users/${id}`)
if (!response.ok) throw new Error(`HTTP ${response.status}`)
return response.json() as Promise<User>
}
The as Promise<User> assertion documents the expected shape but does not validate it.
Warning
response.json() returns Promise<any>. Real code should validate the response body against a schema before trusting it.
Handling errors¶
Errors in async functions are untyped and come through as unknown.
async function load(): Promise<string> {
try {
const user = await fetchUser('1')
return user.name
} catch (error) {
if (error instanceof Error) return `Failed: ${error.message}`
return 'Failed with an unknown error'
}
}
Promise helpers¶
Promise.all preserves the tuple of types when the input is a tuple.
Return type annotation¶
Always annotate the return type of public async functions. It makes the contract clear and catches mistakes.
async function listCourses(): Promise<Course[]> {
const response = await fetch('/api/courses')
if (!response.ok) return []
return (await response.json()) as Course[]
}
Hands-on: typed fetch wrapper¶
- Create
src/api.ts. - Define
interface Todo { userId: number; id: number; title: string; completed: boolean }. - Write
async function fetchTodo(id: number): Promise<Todo>usingfetchandhttps://jsonplaceholder.typicode.com/todos/${id}. - Add a check for
response.okand throw a typedError. - In
src/main.ts, callfetchTodo(1),console.logthe title, and catch errors. - Run with
npx tsx src/main.tsor compile withnpx tscand runnode dist/main.js. - Change the
Todoshape to something wrong and observe the assertion still compile. Discuss why validation is safer than assertion.