Write a typed async fetchUser with error handling

Tests promise-based fetch plus TypeScript return-type contracts. A strong answer checks response.ok before response.json() and types the return as Promise<User>. A red flag is swallowing 4xx/5xx errors silently.
This question tests whether you understand that fetch only rejects on network failure, not HTTP error statuses, and that TypeScript does not automatically validate runtime JSON shapes. A solid answer awaits fetch, throws if response.ok is false, then parses json() and returns the typed result. It should also mention that without the ok check, a 404 would silently return an invalid User. Red flags include omitting the Promise<User> return type, forgetting that json() is async, or relying on fetch to throw on 4xx responses.
Read the original → developer.mozilla.org
- #typescript
- #fetch-api
- #async-await
- #error-handling
- #promises
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.