Understanding fetch in JavaScript

Fetch is like a messenger between your browser and a server. Imagine you’re at a restaurant and you send a waiter (fetch) to deliver your order (the request) to the kitchen (server). Once the food (response) is ready, the waiter brings it back to you. Fetch operates in a similar way, allowing your browser to make HTTP requests and handle responses.

In this lesson, we’ll explore the fetch API and how it simplifies asynchronous server communication.


Quick Overview of the Fetch API

At its core, fetch is used to make HTTP requests in the browser. Unlike older approaches, fetch leverages Promises to manage the asynchronous nature of requests and responses.

Think of fetch as a promise-based way to ask a server for information (like placing a to-go order at a café). Fetch handles the order, waits for the kitchen (server), and delivers your result—all while letting you continue with other tasks.

The fetch API is natively available in most modern browsers, so you can try it out directly in the browser's "Console" tab (e.g., in Chrome or Firefox). However, it’s not built into Node.js, so you can only use it in browser-based JavaScript environments.


fetch Function Syntax

The fetch function has the following syntax:

fetch(url, [options])

- url: The URL to which the request is sent (required). - options: An optional JavaScript object specifying additional request details like method, headers, and body.

If you only pass the url, fetch defaults to making a GET request. Let’s break this down with examples.


Send a GET Request Using Fetch

A GET request is like asking a server for a menu at a restaurant—you’re retrieving information without changing anything.

fetch("/categories/beauty/products")
  .then(function(res) {
    console.log("Response: ", res);
    return res.text();
  })
  .then(function(data) {
    console.log("Data: ", data);
  });

Here’s what happens step-by-step:

Think of this as asking for a menu (request), getting the menu card (response), and then reading it (handling the body).


Send a POST Request Using Fetch

A POST request is like placing an order at the restaurant—you’re sending information to the server to create or modify data.

fetch("/products/1/reviews", {
  method: "POST",
  headers: {
    "Content-Type": "application/x-www-form-urlencoded"
  },
  body: "comment=works+well&starRating=4"
})
  .then(function(res) {
    return res.text();
  })
  .then(function(data) {
    console.log(data);
  });

What’s happening here:

Think of this as placing your order (request), providing details (body), and getting a receipt (response).


What You’ve Learned

You’ve now seen how the fetch API makes it easy to send HTTP requests and handle responses in the browser. With fetch, you can:

This is just the beginning. Later, you’ll learn how to integrate fetch into the full AJAX cycle to update web pages dynamically without a full page reload. This technique forms the backbone of modern web applications, providing faster, more interactive experiences.

Think of fetch as your personal courier for server communication. Whether you’re retrieving or sending information, it ensures smooth and efficient interactions between your browser and the server.