Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

193
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda