I start by saying that I am not 100% sure this is the problem, I mean using await and async.
This is the scenario:
I run this when I first load the page, and
Referring to this article should take care of your issue. See the snippet as well.
async function exampleFetch() {
const response = await fetch('https://reqres.in/api/users/2');
const json = await response.json();
console.log(json);
}
exampleFetch()
await
substitutes for .then()
, so when using await fetch
, you don't need to use .then()
at all.
Here are a couple other answers which deal with more or less the same issue:
1 - How can I acces the values of my async fetch function? [duplicate]
2 - Fetch API Using Async/Await Return Value Unexpected [duplicate]