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.