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

How to Add a Custom Tab to the Customer Order Detail Page in Magento 2

Spread the love Adding a custom tab to the Order Detail

Exception Handling in Java

Spread the love Introduction – Exception:An unwanted

How to add custom less file in Magento2

Spread the love Source files *.css included into layout

Spring Boot Microservices

Spread the love Microservices architecture has become

Implementation of WireMock for better testing

Spread the love WireMock is a flexible and powerful tool

Magento 2: How to selected default option in configurable options?

Spread the love Configurable products in Magento 2 allow

How To Implement Simple JWT Authentication in Django Rest Framework

Spread the love We’ll learn how to use simple JWT

Optimizing Search: Integrating Elasticsearch in Django Applications

Spread the love What is Elasticsearch? Elasticsearch is a

How to create Customer Segments

Spread the love In Magento 2, “customer

Proxy Design Pattern and Requisition List in Magento 2

Spread the love Requisition List in Magento 2 Requisition

bluethinkinc Blogs