fetch() API returning undefined when logged with console.log()

4 days ago 9
ARTICLE AD BOX

I am trying to fetch data from an API using the JavaScript fetch() API and then print the result using console.log(). However, when I log the fetched data, it prints undefined instead of the expected response.

I expect console.log() to display the fetched data, but it runs before the API response is received.

Here is a simplified version of my code:

let data; fetch("https://api.example.com/data") .then(response => response.json()) .then(result => { data = result; }); console.log(data);
Read Entire Article