Returning data from fetch async await issue

This is unrelated to a specific lesson but thought I’d throw it out there. I’m having trouble returning data from a JS fetch using the async / await approach. Here is my code, ideally I’d assign the getData() function to a variable and then be able to pull the data I’m looking for from the variable object, but that isn’t working. Please see code below and the log.

async function getData() 
        {
            //await the response of the fetch call
           let response = await fetch('https://nwis.waterservices.usgs.gov/nwis/iv/?format=json&indent=on&sites=04288230,01134500&parameterCd=00060&period=P1D');
            //proceed once the first promise is resolved.
           let data = await response.json()
            //proceed only when the second promise is resolved
		   return data;
        }

//call getData function
let infoObj = getData();
console.log(infoObj);

Capture

Like your screenshot shows, the result of calling your function is a promise.
What can you do with a promise?

So I think I’ve got this sorted out to an extent.

I cannot return the promise’s resolved value to a variable to then be used later in my code due to the asynchronous nature of the promise. To achieve the desired result of “doing something” with the value returned in the promise I need to pass it into a callback function with the .then() method.

So when reviewing the initial code I had listed the next step is to use the .then() method to pass the resolved value into a callback function.

so in this case

function doSomething (resolvedValue) {
// code to do something here with the resolved value
}

infoObj.then(doSomething)

Still learning more - just on the promises lessons now, working through it to better understand and clean up my code, but already I have the project I was working on running.

Every time one of your functions (including when your code runs the first time) is called, you regain control. Each time they finish they are yielding to the event loop again.

What you cannot do is to refuse to yield to the event loop, and yet have the eventloop run and therefore produce a result. You have to yield for the result to resolve.

If you had a second thread, then one thread could run the event loop while the other thread never yields, so the non-yielding thread could send an async action to the event thread and then collect it later on. You don’t have a second thread. You could start a second process and have them communicate in this fashion. But. You also don’t have blocking operations so I guess you’d have to use a loop to busy-wait.