Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

233
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda