Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialEwerton Luna
Full Stack JavaScript Techdegree Graduate 24,031 PointsIn Guil's example, what's the reason for using await on the response.json( ) expression?
Hey, everyone. I don't get why do we have to place a await keyword in front of response.json( ). Doesn't then .json( ) method parses the response into a JSON object and returns this object wrapped into a resolved Promise? This will happen synchronously, won't it? I don't see any reason why parsing a response would run asynchronously if the response object already contains the fetched data.
Thanks!
2 Answers
Seth Feingold
Front End Web Development Techdegree Graduate 19,316 PointsI believe this is because the async/await keywords are effectively replacing the "then" method one would usually chain onto the fetch method.
So instead of:
fetch(url).then( response => console.log(response.json()) )
We are awaiting the 'response' from fetch(url) and assigning it to a variable, "response". Then because that process is also asynchronous, we are awaiting that response variable to be assigned before parsing it into JSON and assigning it to a new variable, "data".
Hopefully this is helpful!
JingLin Chen
6,998 Points我也有這個問題,謝謝兩位