Hello, I just got into the APIs part of Javascript
The instructor tells me to google about “how to use fetch with json”. So I did, but the mdn doc ( Using the Fetch API - Web APIs | MDN ) doesn’t mention anything about .then(). It seems to be updated and only use async await now. I’m pretty sure this will confuse a lot of ppl learning about it, because it confuses me too.
Shouldn’t this be updated with clarification? Now I have no idea whether I should use fetch then or async await.
Both styles work because fetch returns a Promise so you can handle that with .then() or await. The difference is in readability and also how JavaScript engines can optimize them.
With .then(), the browser has to save extra info about where errors came from, which uses memory and can slow thing down.
I assume that’s why MDN doesn’t have it. Async is easier to read and it gives browsers more room to optimize. Still, it’s worth learning .then() first to understand how Promises work under the hood but in your own code, async/await is usually the better default.
But still, the MDN reference/screenshot in that lesson is outdated. I just thought it’s better to briefly explain/clarify about the current MDN reference (async await) being different to people who’s learning about it, to prevent confusion.
Thanks Jonathan
I’m aware that the async section could do with updating and it is on my list! I would like it to focus more on the modern async/await syntax.