Categories
JavaScript Answers

How to read the body of a Fetch promise with JavaScript?

Spread the love

Sometimes, we want to read the body of a Fetch promise with JavaScript.

In this article, we’ll look at how to read the body of a Fetch promise with JavaScript.

How to read the body of a Fetch promise with JavaScript?

To read the body of a Fetch promise with JavaScript, we can use the json method.

For instance, we write

const f = async () => {
  const response = await fetch("https://api.ipify.org?format=json");
  const data = await response.json();
  console.log(data);
};

to define function f that makes a get request with fetch.

Then we call response.json to return a promise with the response body object.

Finally, we use await to get the response body object and assign it to data.

Conclusion

To read the body of a Fetch promise with JavaScript, we can use the json method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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