Fetch API in JavaScript

Blog
Spread the love

In this blog, we will study about fetch API. The Fetch API is a web API that allows JavaScript to make HTTP requests to servers in order to retrieve or send data. It provides a way for developers to fetch data from a network, or send data t
o a server, by making HTTP requests using a simple and flexible interface. This API is commonly used for retrieving data from a server or sending data to a server in order to update or create resources.

In this article, we will learn –

How convenient is it?

How does it work with promises?

How can you send network request using JavaScript?

Let’s move on to the blog.

Here’s to explain you about fetch API…

Firstly, we will search for free API and here I’ve got a repo in GitHub.

github_page

Here are some public APIs based on distinct themes.

distinct_themes
distinct_themes

And below it states whether you want to have authentication.

whether_themes

We can use any API as such in the above slide where Weather API is used as an example.

Now here we’ve got an API in which authentication is not required.

And for demo I’m using API from the link above.

Syntax:

Here p is a promise (JavaScript Concept) and Fetch API returns a promise.

How can you make this syntax easy for you?

Whenever we fetch anything we get nested promises in which .then(first one) is returning response in JSON and again .then(second one) is logging the response in the console. Thus, two promises are taken at minimum for efficient flow.

Why is it two stage process?

So, fetch is used to get data over network. It is two stage process because

Stage -1 : Firstly, we get a response as an object which contains two properties…

Status- HTTP status code will be there inside it (e.g. 200).

Ok- If status code is between 200-299, it will give output true if everything is alright.

Syntax:

Firstly .then(first promise) is resolved, we log response.status and response.ok into the console and then returning the data. And again .then(second one) is simply logging the response onto the console.

So the output will have:

200

True

and lastly all the data present in the API.

data_present_img

Response can be big and it may take time. So, that’s why it is a two-stage process. If you get error in first .then and if you don’t get any response then you resolve here within the same .then promise.

After first .then is resolved this will return a promise and the promise is response.json() which will resolve in json response. That’s why we use two .then.

There are many more methods to access the body in different format-

i.response.text() : It return the response and read as text

methods_access_img

ii. response.json() : It returns the response as JSON (JavaScript Object Notation)

response_json_img

iii. response.blob() : It returns the response as binary data blob(Binary Large OBject).

response_as_binary

iv. response.arrayBuffer() : It returns response as the binary data(low-level representation)

representation_img

Conclusion

I hope this blog found out to be useful for you. If you have queries feel free to ask in the comments below.

Fetch API is a powerful concept with which we can use data from any network.

Keep learning.

Thank you!

bluethinkinc_blog

bluethinkinc_blog

2023-03-29

0

Leave a Reply

Your email address will not be published. Required fields are marked *

Find More Blogs

Proxy Design Pattern and Requisition List in Magento 2

Spread the love Requisition List in Magento 2 Requisition

Google Ads Conversion Tracking Through Magento Platform:

Spread the love Google Ads Conversion Tracking Through

Object Pool Pattern in Magento2

Spread the love What is Object Pool Pattern in Magento 2 An

JENKINS

Spread the love JENKINS Introduction and Installation

Virtual Type and Type

Spread the love Virtual type is one of the most common

Customize the length of Input field

Spread the love Sometimes we need to change the limit the

How to Add Custom Tab on Admin Dashboard and Count Website Total Visitor in Magento 2?

Spread the love In this blog we’ll see How to Add Custom

How to Add Sign Out Tab in Customer Account in Magento 2?

Spread the love In this blog we’ll see How to Add

How to create custom tab in product detail page in magento 2?

Spread the love In this blog we’ll see how to add

Understanding and Implementing CORS & CSRF

Spread the love What is security? The world has become more

bluethinkinc Blogs