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

115
Visualizações
How to wait for the completion of the entire process of a function called by events?

I have a function that is triggered by an event, is there any way to wait for the completion of the entire process of this function, even if the event is triggered other times, so that only after that it continues processing the other triggers received?

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

0

As far as I understood you need some sort of a queue. You need to push events there and process them one by one. Maybe something like this:

const queue = [];

function eventHandler(event) {
  queue.push(event);
  if (queue.length === 1) {
    doTheJob();
  }
}
async function doTheJob() {
  if (queue.length > 0) {
    const event = queue[0]
    // do the job with the "event"
    await work1();
    await work2();
    await workN();
    queue.shift();
    doTheJob();
  }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

I managed to find a solution like this:

var time = 1;
// SIMULATES THE ACTIVITIES OF THE EVENT
setInterval(async () => {

  await process();

}, 1000);

// THE PROCESSING THAT OCCURS WHEN THE EVENT IS TRIGGERED
async function process() {

  time += 4000
  const myPromise = await new Promise(async function (myResolve, myReject) {

    setTimeout(async () => {
      console.log(`Process executed`);
      myResolve("Process completed");
    }, time);

  });//End myPromise
  var result = await myPromise;
  time = time - 4000
}

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