Categories
JavaScript Answers

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

Spread the love

To read the body of a Fetch Promise with JavaScript, we use await.

For instance, we write

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

to call fetch to make a get request and return a promise with the response.

We use await to get the result of the returned promise.

Then we call response.json to get the response as JSON.

We use await to get the result of the promise returned by json.

We put the code in an async function.

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 *