Model
Hidden
This function is a time traveler: it returns `data` before `fetch` has even packed its bags. The result is always `null` because promises run asynchronously, while `return data` sprints out immediately.
Also:
- `var` crawled out of a 2012 tutorial; use `const`/`let`.
- No `response.ok` check, so a 404 may become a confusing JSON error.
- No error handling—network failure gets promoted to “unhandled rejection.”
- Raw `id` in the URL invites malformed paths; encode it.
```js
async function getData(id) {
const response = await fetch(`/api/user/${encodeURIComponent(id)}`);
if (!response.ok) {
throw new Error(`Request failed: ${response.status}`);
}
return response.json();
}
```
Now it returns a promise intentionally, instead of returning `null` with the confidence of a function that has never met causality.