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

141
Views
No es posible obtener un índice en un ciclo/función asíncrona

Traté de obtener un índice que no obtengo de inmediato, pero no sé cómo hacerlo correctamente.

Creé un ejemplo rápido donde hay 2 botones y accediendo a ellos. Pero siempre obtengo solo 0 index.

 const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); const btns = document.querySelectorAll('button') const btnsArr = [...btns]; let index = 0; const asyncTestFunction = async () => { for (let i = 0; i<2; i++) { await sleep(5000); index = i; console.log(index, 'loop'); } } asyncTestFunction(); btnsArr[index].addEventListener('click', () => { console.log(index, 'index - EventListener') })
 <html> <head> <meta charset="UTF-8"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <button >My other page click1</button> <button >My other page click2</button> <script src="script.js"></script> </body> </html>

No obtengo el evento del segundo botón.

Esto es por cuestión de tiempo, no estoy esperando a que me llegue el índice porque no sé cómo hacerlo correctamente.

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