I've been following a tutorial and came across the following code snippet:
const myAsyncFunction = async () => {
const usersResponse = await fetch(
'https://jsonplaceholder.typicode.com/users'
)
const userJson = await usersResponse.json();
const secondUser = userJson[1];
console.log(secondUser);
const posts = await fetch (
'https://jsonplaceholder.typicode.com/posts?userId=' + secondUser.id
);
const postsJson = await posts.json();
console.log(postsJson);
}
myAsyncFunction();
Shouldn't the converting of a response to a JSON object happen instantly, the same way fetching a value from an array e.g. userJson[1]
does? Why is it required to await
usersResponse.json()
and posts.json()
?