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

176
Views
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 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report
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!