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

85
Visualizações
Javascript Firebase RealTime Database Completion Block

I am trying to understand firebase-realtime-database. I am able to fetch the data, but I do not know how to add a completion block. I mean, is there a function to check when my data fetch query is completed?

function getData() { 
  firebase.database().ref('SectionNames').once('value', function(names) {    
    names.val().forEach(function(sectionname) { 
      firebase.database().ref('Sections').child(sectionname).once('value').then( function(child)  

      //code

    });   
   });
  });
 //Completion block or a method to call processData() after I get all the sections
}

function processData() {
 //call this function after you get all the sections
}

Thank you very much!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The once() method also returns a promise, so you can use the usual promise handling logic (then()/catch() or try/catch).

For example:

function getData() async { 
  let names = await firebase.database().ref('sections').once('value');
  processData(names);
}

Update: since you now updated your code to show there are multiple once calls in a loop, you can use a Promise.all for that:

function getData() { 
  firebase.database().ref('SectionNames').once('value', function(names) {    
    let promises = [];
    names.val().forEach(function(sectionname) { 
      promises.push( 
        firebase.database().ref('Sections').child(sectionname).once('value')
      );
    });   
    Promise.all(promises).then((snapshots) => {
      processData(snapshots);
    });
  });
}
about 4 years ago · Juan Pablo Isaza Relatório
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