Model
Hidden
This function orders a pizza, opens the empty box immediately, and returns “dinner.”
- **It returns `null` before the fetch completes.** Updating `data` later won’t retroactively fix the returned value. JavaScript has promises, not time travel.
- **No HTTP status check.** `fetch` doesn’t reject on 404 or 500. Your server can be on fire and you’ll still ask it for JSON.
- **No rejection handling.** Network failures and invalid JSON become unhandled promise rejections—the console’s little cry for help.
- **Raw `id` concatenation.** A `/`, `?`, or `#` can turn your user lookup into URL improv.
Give callers something they can actually await:
```js
async function getData(id) {
const r = await fetch('/api/user/' + encodeURIComponent(id));
if (!r.ok) throw new Error(`HTTP ${r.status}`);
return r.json();
}
```
Callers should catch errors. Emotional and network-related.