How do I make a loop only continue after an if is performed!

Asked

Viewed 65 times

0

I am programming a bot for Discord that asks questions and checks if they are right, but the doubt that this taking nn is with the lib discordjs itself but with the language itself!! And the following one: He asks the question but to ask the next question and I need this first question to be answered first! To know the answer I use the reactions which is a feature of Discord, and then I get the number of votes on that question and release only when the variable is changed To monitor this variable I need to use a setInterval structure and when it is different from 1 it executes the code! But the loop continues as I get the questions from an array! How can this loop that extracts the questions from the array continue after this variable is changed??

here goes the crucial third of the code

 for (const i in PerguntasEmbaralhadas) {
          var pergunta = PerguntasEmbaralhadas[i]
          var exec = true
          var questao = await msg.channel.send({ embed: pergunta });

          const Alternativas = { 1: '', 2: '', 3: '', 4: '' }
          for (const a in Alternativas) {
            await questao.react(Alternativas[a])
          }
        }

        const revisor = await setInterval(async () => {
          var countAnwser = await questao.reactions.cache.map(reaction => reaction.count)
          for (const i in countAnwser) {
            if (countAnwser[i] != 1) {
              await msg.reply(`Sua resposta foi a alternativa ${i + 1}`)
              clearInterval(revisor)
              semaforo = true
            } else {
              bsemaforo = false
            }
          }
        }, 1000)
      }

thank you so much

  • place the piece of code inside a while and control with a variable, for examplevar continuar = true; while (continuar) { ...aqui vai o codigo, quando a pergunta for repondida, faça continuar = false }

  • All post code or subtitle for while?

  • put in while the code asking first question, so it will remain in that while until the question is answered

  • 1

    As you are working with asynchronous reactions, this can be a little more complicated than you might imagine. Also, utilize a setInterval is far from ideal in this case. Perhaps it is the case that you search for events and perhaps about reactive programming.

  • Is there another simple method, where I can monitor these variables and perform some function as soon as it changes??

1 answer

-1

You can try working with Yield, you run a loop and return an object that has the next item of the loop method next() see here

The keyword Yield is used to pause and summarize a Generator Function (Function* or Generator Function legacy).

Example of documentation:

function* foo() {
  var index = 0;
  while (index <= 2)
    yield index++;
}

var iterator = foo();
console.log(iterator.next()); // { value: 0, done: false }
console.log(iterator.next()); // { value: 1, done: false }
console.log(iterator.next()); // { value: 2, done: false }
console.log(iterator.next()); // { value: undefined, done: true }

Source here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.