Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!