DEV Community

Cover image for Fetch API is new old version of AJAX

Fetch API is new old version of AJAX

Diona Rodrigues on May 04, 2022

If you started your developer career about a decade ago, you probably know how complicated it was to use AJAX in our web applications. However, by ...
Collapse
 
camco profile image
Camco

Super helpful

Collapse
 
dionarodrigues profile image
Diona Rodrigues

Thank you!

 
vetras profile image
vetras

Well, you are not wrong because this is pretty much a personal preference.
I just do not agree with you. 😎

Hiding async code with syntax sugar
It's not hiding, it's making it clear that it is async.

with async/await you have to wrap stuff in a try/catch
Only if you can handle the error and know what to do, otherwise it is better to let it bubble up.

Lastly, all the possible options you mention although correct, I think they miss the point.

If you have this:

fetch("someapi.com")
    .then(foo);
bar();
Enter fullscreen mode Exit fullscreen mode

The point is to refactor into async/await but keep the exact same behaviour.
It does not matter much what that behaviour is on the example (functions don't even have a body here).

Cheers! 🍻 happy coding

Collapse
 
vetras profile image
vetras

I don't think it is easier to read. Besides if you have this:

fetch("someapi.com")
    .then(foo);
bar();
Enter fullscreen mode Exit fullscreen mode

The order of execution requires you to know that there is a promise here so that the functions are called in this order:
1- fetch
2- bar
3- foo

Collapse
 
xuanchinhh97 profile image
Xuan Chinh

helpful

Collapse
 
dionarodrigues profile image
Diona Rodrigues

Thank you!

Collapse
 
jnareb profile image
Jakub Narębski

Can you get progress information and partial results with the fetch API?

Collapse
 
dionarodrigues profile image
Diona Rodrigues

Hey Jakub, I think I didn't get your questions. Can you explain it a little more?

Collapse
 
jnareb profile image
Jakub Narębski

With AJAX in onreadystatechange you can not only check whether you got full response, or an error, but also if you got partial response. It is useful for generating progress information if the response is slow (you receive results in spurts) - but of known final size, or for generating partial results.

Collapse
 
koas profile image
Koas • Edited

Hi Jakub, for progress and partial results Server Sent Events (SSE - developer.mozilla.org/es/docs/Web/...) may be what you’re looking for.

Collapse
 
mehuge profile image
Mehuge

Typo in the last example?

getUserData().catch(err => console.log(err))
Enter fullscreen mode Exit fullscreen mode
Collapse
 
dionarodrigues profile image
Diona Rodrigues

Hey Mehuge, what do you mean by Typo?

 
dionarodrigues profile image
Diona Rodrigues

Hey @lukeshiru and @vetras, thanks for sharing your points. I think both ways are completely fine and I also think it depends on personal/team preferences.

Collapse
 
hesamdanaee profile image
Hesam

So helpful. Thx

Collapse
 
dionarodrigues profile image
Diona Rodrigues

Thank you!

Collapse
 
kosoko_daniel profile image
Oluwagbenga

Thank you

Collapse
 
dionarodrigues profile image
Diona Rodrigues

You're welcome!! :)

Collapse
 
nicholaskimuli profile image
Nicholas Kimuli

Thank you!

Collapse
 
dionarodrigues profile image
Diona Rodrigues

You're welcome!! :)

Collapse
 
andrewbaisden profile image
Andrew Baisden

Really good article with lots of info.

Collapse
 
dionarodrigues profile image
Diona Rodrigues

Thank you!

Collapse
 
jumashafara profile image
Kibekityo Juma Shafara

Very well organized, easy to understand thanks, am following you 😌

Collapse
 
dionarodrigues profile image
Diona Rodrigues

I appreciate it. Thank you! :)

Collapse
 
madrus profile image
Madrus

Thanks for putting it all together in one concept pointing out all the different possibilities. Very clear and to the point.

Collapse
 
dionarodrigues profile image
Diona Rodrigues

Thank you for your comment. I'm really glad you liked it.