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

253
Visualizações
How to run the same JS function multiple times and wait until the previous has finished?

I have a function that I need to run multiple times but wait until the previous one has finished.

Currently What I have is:

for(var x = 0; x < rollsArr.length; x++){
    rollDice(data.results, x)
}

function rollSkins(results, rollN) {
    // do some stuff

    setTimeout(function() {
        roll(results, rollN)
    }, 500);
}

function roll(results, rollN) {
    // do some stuff

    setTimeout(function() {
        // do some stuff
    }, 8500);
}

I would like a roll function to finish then wait a few seconds and then start another roll. I tried awaiting rollDice but I didn't implement it correctly or it did not work.

What would be the solution for this?

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

0

You can use async/await or Promise.then(). Reference: https://zellwk.com/blog/async-await-in-loops/

const rollsArr = [1,2,3];
const data = {results: "example"};

const sleep = ms => {
  return new Promise(resolve => setTimeout(resolve, ms))
}

(async () => {
  for (var x = 0; x < rollsArr.length; x++){
      await rollDice(data.results, x)
  }
})()

async function rollDice(results, rollN) {
    // do some stuff

    await sleep(500);
    await roll(results, rollN);
}

async function roll(results, rollN) {
    // do some stuff
    console.log("Rolling dice");

    await sleep(1000);
    console.log("Doing stuff");
}

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