JavaScript Fetch API Made Easy

Hello everyone, I hope everyone is safe.

In this blog, we will learn about fetch() API with a simple example.


A basic understanding of Promises, Async, and Await is required.

What is Fetch API?

  1. The Fetch API is a modern interface that allows us to make HTTP requests to servers from web browsers.
  2. And it is much simpler and cleaner, uses promise to deliver more flexible features to make requests to servers from the web browsers.

Let us see sending a request and reading the response.

Sending a Request

The fetch() requires only one parameter which is the URL of the resource that you want to fetch,

let response = fetch(url);

The fetch() method returns a Promise so you can use then() and catch() methods to handle it,

 .then(response => {
    // handle the response
 .catch(error => {
    // handle the error


.then(res => console.log(res));

	 JavaScript Fetch API Made Easy

In the above example,

  1. When the request completes, the promise will resolve into a Response object.
  2. The Response object has a number of useful properties and methods to inspect the response

Reading the Response

In practice, we use the async / await with the fetch() method,

async function fetchMethod() {
    let response = await fetch(url);
    let data = await response.text();

Now let us build a simple application that gives us a clear picture of fetch API using "reqres", a fake data API - - know more here.

Step 1 - Build the Client-Side

    <h1>Fetch API Basics</h1>
    <h2>GET Method</h2>
    <div class="container">
        <!-- Fetch API -->
    <script src="./fetchAPI.js"></script>

Step 2 - Define the method

getUsers() method will return response data from the API.


async function renderUsers() {
    let users = await getUsers();//Fetch method

Step 3 - The fetch() method

async function getUsers() {
    let url = '';
    try {
        let res = await fetch(url);
        return await res.json(); //Reading response
    } catch (error) {

Besides the json() method, the Response object has other methods such as text(), blob(), formData() and arrayBuffer().

Step 4 - Reading and displaying the response data

Full code for renderUsers() method,

async function renderUsers() {
    let users = await getUsers();
    let htmlContent = '';
    let usersLen =;
    for(var i=0;i<usersLen;i++){
        let htmlSegments = `<div class="user"><img src="${[i].avatar}" >
        <h2>${[i].first_name} ${[i].last_name}</h2>
        <div class="email"><a href="">${[i].email}</a></div> </div>`;
        htmlContent += htmlSegments;
    let container = document.querySelector('.container');
    container.innerHTML = htmlContent;


	 JavaScript Fetch API Made Easy

To know more, do read the official documentation.

Happy learning!

Expertise in the cloud, speed of innovation and customer focus on building strong relationships