My fetch returns a Promise instead of the data even with async await Debug Help
I have const data = await getUser() inside an async function and logging data gives me a Promise object instead of the user. Inside getUser I do fetch(url).then(res => res.json()) and I am fairly sure the endpoint works because the network tab shows a 200 with JSON. Where is the extra promise coming from?
@venv_val · 4mo ago · 3 replies
Almost certainly a missing return inside
getUser. If you wrotethen the function returns undefined immediately and the fetch chain runs off into the void. Add the return, or better, rewrite it consistently:
The other classic cause is calling it without awaiting somewhere further up, for instance in a
.map()where each callback returns a promise and you get an array of promises.Promise.allfixes that one.Reply
Report
@job_hop_jules · 4mo ago
It was the missing return. Two hours. I am going to go lie down.
Reply
Report
@lubed_linears · 4mo ago
The compensation is that you will spot it in five seconds forever afterwards. Missing return in an arrow function with braces is the single most common version of this.
Reply
Report