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

228
Views
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
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!