I am trying to write a function that I can reuse to make different fetch requests in my react app. But I am getting unexpected null value and the order in which the function is executed is confusing me. In my code bellow I log the data value before i assign it to my temp variable and which prints the expected output, but the temp variable prints as null - It should also be noted that the temp variable prints BEFORE the data prints even though I have printed the temp variable afterwards... I am calling this function in the componentDidMount() method of the app.js component. Anyone know why I am unable to assign the data from my fetch request to a variable and return it?
testDynamicAPI(e) {
var temp;
fetch(e)
.then(response => response.json())
.then(data => {
console.log(data);
temp= data
});
console.log(temp);
return temp;
}