I am using a code example I found using fetch, async, await. I am having trouble figuring out how to substitute a .then in the code for another await.
Here is the code
async function getData(url = "", data = {}) {
// Default options are marked with *
const response = await fetch(url, {
method: "GET", // *GET, POST, PUT, DELETE, etc.
headers: {
"Content-Type": "application/json",
},
});
return response.json(); // parses JSON response into native JavaScript objects
}
const response = getData(
"https://site.api.espn.com/apis/site/v2/sports/football/nfl/scoreboard"
).then((data) => {
console.log(data.leagues[0].name);
});
I tried
const response = await getData(
"https://site.api.espn.com/apis/site/v2/sports/football/nfl/scoreboard"
)
But got an error await is only valid in async functions and the top level bodies of modules
What should I do?