DEV Community

Cover image for API Handling in JavaScript & React: From Basics to Pro-Level

API Handling in JavaScript & React: From Basics to Pro-Level

Shubham Tiwari on March 10, 2025

Learn JavaScript api handling with fetch, axios, and error handling. API handling refers to making HTTP requests to a server to fetch or send data....
Collapse
 
webjose profile image
José Pablo Ramírez Vargas

I'm on a quest against if (!response.ok) throw....

The Ugly Truth: All Popular fetch() Wrappers Do It Wrong

Discover the reality behind axios, ky and popular data-fetching NPM packages and how to replace them.

favicon webjose.hashnode.dev
Collapse
 
shubhamtiwari909 profile image
Shubham Tiwari

Try catch is an important wrapper as it avoids the termination of the app if there is an exception during data fetching, best example would be data fetching in next js, If you don't use try catch and an exception occurs, your webpage will crash

Collapse
 
webjose profile image
José Pablo Ramírez Vargas

I'm not against generic try..catch. I'm against throwing an error when a non-OK response is received via fetch() because then you are forced to write an extra try..catch.

Thread Thread
 
shubhamtiwari909 profile image
Shubham Tiwari

Yeah, that is just an example, in a real world project, we will be adding an error state and a UI for that exception.

Thread Thread
 
z2lai profile image
z2lai

I thought this article was about going pro! Anyway, what about retry logic, have you seen that implemented in the real world?

Thread Thread
 
webjose profile image
José Pablo Ramírez Vargas

@z2lai are you asking the author or me? Apologies. The notification showed up in my feed. The author, right?

Thread Thread
 
shubhamtiwari909 profile image
Shubham Tiwari

This article is about API handling only in multiple ways.
There are too many examples and concepts that can't be covered in 1 blog.

Collapse
 
shubhamtiwari909 profile image
Shubham Tiwari

This article is focusing on how we could handle the api like fetch method for beginners, axios method and react useEffect for intermediate and React query for pro
It's not possible to go in depth about these covering all the concepts in detail in a single article.
You could refer the official documentation for these concepts to learn more about other implementations like pagination, infinite scrolling, retries, dynamic data fetching, refetch on operation, etc.

Collapse
 
aniruddhaadak profile image
ANIRUDDHA ADAK

Incredibly helpful, thank you!

Collapse
 
andrewbaisden profile image
Andrew Baisden

Nice, this is a great overview. Pretty sure I have used each method in different projects.

Collapse
 
gopisuvanam profile image
Gopi Krishna Suvanam

thanks for the lovely introduction..