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

177
Visualizações
Problems with async/await and promise using setTimeout

I've the following code that should have to works for a slider:

let timeOut, timeOut2;
over = 1;
let back = 0;
setInterval(change2, 8000);

 async function change2() {
  innerFront = slides[over];
  innerBack = slides[back];
  await setTimeout(change, 8000);

  innerFront.querySelectorAll('figure')[0].classList.remove('isActive');
  innerFront.classList.remove('slideshow__inner--front');
  innerBack.querySelectorAll('figure')[0].classList.remove('isActive');
  innerBack.classList.remove('slideshow__inner--back');
  over -= 1;
  if (over < 0) over = slides.lenght;
  back = over - 1;
  if (back < 0) back = slides.lenght;
  slides[over].classList.add('slideshow__inner--front');
  slides[back].classList.add('slideshow__inner--back');
}

function change() {
  return new Promise(function(resolve, reject) {
    innerFront.querySelectorAll('figure')[0].classList.add('isActive');
    timeOut2 = setTimeout(()=> {
      innerBack.querySelectorAll('figure')[0].classList.add('isActive');
    }, 1000);
    resolve();
  });
}

My problem consists in the fact that the 'change' function seems not to be performed despite the Await for Promise. In fact, the subsequent instructions are immediately performed, and obviously this generate errors. I still have doubts about the promises, which I am studying, and here I think there can be something conceptual. Does anyone have the patience to explain to me where I'm wrong?

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

0

There's a typo inside of change2() you wrote lenght instead of length twice.

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