here is my code in github. it is working now with fetch api. but i have tried with async await you will see the commented code there. it was also returned a promise. so if i have to to call .then() again why should i use async await instead of normal fetch procedure.
Ok, I see what you mean now. The reason you needed to use the .then
method on the response, is because the resp.json()
is also a promise. So you need to do this
const resp = await fetch(url)
const data = await resp.json()
console.log(data)
So whenever you see a Promise {<pending>}
result logged to the console, then you also need to await
that, or else use you can revert back to using .then
.
The reason I recommended to use asyc/await
is that it is more readable than nested promises, and allows you to reason about the logic as if it was synchronous. In other words, you can think of the logic as stopping until the promise returns.
Hope that helps. If there’s something I didn’t make clear, let me know and I’ll elaborate.
“First we resolve the actual network request, then we resolve the json parsing (which is also an async operation like fetch itself).”
Just … WHAT!!!
WHY??
Who the hell designed it this way?
Of all the people who deserve a smackdown!!
Thanks, your example helped me fix my code also; was just missing the second ‘await’. The examples in the docs were helpful too if anyone else wants to see more…