A complete Introduction to Fetch API.
Thread ๐งต๐๐ป
Thread ๐งต๐๐ป
Fetch is a web API that provides an interface to fetch resources across the networks.
For making a request, we have the `fetch()` method.
For making a request, we have the `fetch()` method.
The fetch method is entirely based on `promises` that can be resolved to the request's response.
Before moving further, let's talk a little bit about Promises. ๐๐ป
Before moving further, let's talk a little bit about Promises. ๐๐ป
We can say that Promise is nothing but the object that represents the result of an asynchronous call.
The result can either be successful or failure.
The result can either be successful or failure.
A Promise has three states:
Pending: Neither fulfilled nor rejected.
Resolved: Promise fulfilled.
Rejected: Promise rejected.
Pending: Neither fulfilled nor rejected.
Resolved: Promise fulfilled.
Rejected: Promise rejected.
There are two possible scenarios when you fetch resources through an API call.
- The server will return the resource: Promise fulfilled.
- The server will throw an error: Promise rejected.
- The server will return the resource: Promise fulfilled.
- The server will throw an error: Promise rejected.
If you want to learn more about Promise, please take a look here.
developer.mozilla.org
developer.mozilla.org
In the above code snippet, we didn't mention the HTTP request.
If we don't specify the HTTP method, the fetch API will use GET by default.
If we don't specify the HTTP method, the fetch API will use GET by default.
What if we want to make POST, PUT, or DELETE requests?
The fetch method takes a second optional argument as well.
The fetch method takes a second optional argument as well.
With that being said, this is pretty much it for this thread.
Follow @Rapid_API for more exclusive content.
Follow @Rapid_API for more exclusive content.
Loading suggestions...