I cannot figure out how to get data from a fetch response into a new global variable. I am using this question but I don't want to console.log()
the array, but instead put it into a new variable.
Using the linked question, I am doing almost the same:
function getvals(){
return fetch('https://jsonplaceholder.typicode.com/posts')
.then((response) => response.json())
.then((responseData) => {
return responseData;
})
}
let data;
getvals().then(response => {
data=response;
})
I would like the data
variable to hold the array that is return. I thought I would just have to declare a new global variable and then change within the .then
but I get undefined.