Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

42
Views
Global Variables and Callback promies in Javascript

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);

}
7 months ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.