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

230
Visualizações
In Google Firebase RTDB, how do you yield a forEach loop until a certain event happens? (JavaScript)

I'm working on a game like Kahoot and I'm using the forEach function on a DataSnapshot to loop through every question. The problem is that since there's no way to sort of "yield", it just skips to the last question. How could you sort of "yield", and then once a certain action happens, (when they click any of the four options) then just do sort of "continue". I've tried the yield operator in the forEach loop and it DID actually pause at the first question, however doing continue or anything like that would not work. Here is my code:

snapshot.forEach(function(item) {
    var ol = Object.values(item.val().Options)
    var olk = Object.keys(snapshot.val())

    var q = "Question " + olk[questionIndex].split("Question")[1] + ": " + item.val().Question

    document.getElementById("theQuestion").innerText = q

    questionIndex++

    for (var i = 0; i < answerBtns.length; i++) {
        answerBtns[i].innerText = ol[i]
    }

    document.querySelector(".qna-section").addEventListener('click', (e) => {
        if (e.target.nodeName.toLowerCase() != "button") return;

        if (e.target.innerText == item.val().CorrectAnswer) {
           console.log("Question index " + questionIndex + ": Correct!")
        } else {
           console.log("Question index " + questionIndex + ": Incorrect.")
        }

        // I want it to sort of "continue" here.
    })

    // I want to sort of "yield" here.
})

I've been stuck on this for about a week, so help would be appreciated! Thanks.

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