Course contentsShow
← JavaScript
Lesson 2255 of 2,72347. Networking PrimitivesFree lesson

Your First Fetch Call

Making a simple GET request with fetch and handling the returned promise.

Your First Fetch Call

You'll learn how to make your very first HTTP GET request using fetch() and handle the data it returns.

The Basic Pattern

Now that you know what the Fetch API is, let's actually use it! A fetch() call returns a promise that resolves when the server responds. The most common pattern looks like this:

fetch('https://api.example.com/data')
  .then(response => response.json())
  .then(data => {
    console.log(data);
  });

Understanding What Happens

When you call fetch() with a URL, three things happen:

  1. The request is sent to that URL (by default, a GET request)
  2. A promise is returned immediately — your code doesn't stop and wait
  3. When the response arrives, the promise resolves with a Response object

Think of it like ordering food delivery: you place the order (fetch), get a tracking number immediately (the promise), and later receive your food (the resolved response).

Handling the Response

The Response object isn't the actual data yet — it's more like a package that contains the data. You need to "unpack" it:

fetch('https://jsonplaceholder.typicode.com/users/1')
  .then(response => response.json())  // Unpack as JSON
  .then(user => {
    console.log(user.name);  // Now you have the actual data!
  });

The .json() method also returns a promise, which is why we chain another .then().

Key Takeaway: A basic fetch() call takes a URL, returns a promise, and requires calling .json() on the response to extract the actual data — chaining promises lets you handle each step cleanly.