I'm fairly new to JavaScript and having some issues with a global scope (I think) with my react app.
from my API I'm receiving a promise containing the .json information of my API request. From what I've read, I need to use a Callback function to access the Data from that API request. I'm trying to save the .json from that request as a global object but I'm having some issues. I've used an alert which can print the .json just fine from within the callback. However, when I try to save it as a global var, Im getting undefined in my main function. How can I ensure that I can access reservationsFromDB from within ReservationList?
var reservationsFromDB;
function receiveResevations (res){
reservationsFromDB = JSON.stringify(res);
//Here I get correct .json information
//window.alert(reservationsFromDB)
}
const ReservationList = () => {
api.getReservations().then(res => receiveResevations(res));
//here i get undefined
window.alert(reservationsFromDB);
}