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

178
Views
JavaScript: espera en un bucle if else en el orden correcto

Había usado setTimeout(), función de suspensión, asíncrono y espera, pero todos ellos arruinarán mi orden de salida y arruinarán mi ciclo. Todos siguen haciendo cosas en la línea de tiempo hasta que llega el tiempo y luego vuelven a contar los elementos de conteo anteriores. Esto es molesto porque espero que se pueda mantener la secuencia de salida, de lo contrario, todo mi chat de actualización se estropeará. ¡¡¡Ayúdame!!!

mi código:

 class Chatbox { constructor(){ ... } updateChat(chatbox) { const Message = this.messages.slice() allMessage.forEach(function(item,index) { //it will go "else" first then do "if" then do "else"-> "if" -> "else"... if (item.name === "Sam") { console.log("3"); if (True){ console.log("4"); } else{ console.log("4"); } //wait 2 seconds console.log("5"); } else { console.log("1"); //wait 2 seconds console.log("2"); }}}}

mi expectativa:

 "1" //first message (loop) wait "2" "3" "4" wait "5" //second message (loop) "1" wait "2" "3" "4" wait "5"

Salida real si se usa setTimeout():

 "1" //first message (loop) "3" "4" "2" "1" //while waiting output "5", it already start the second loop "5" "3" "4" "2" "1" //while waiting output "5", it already start the second loop which output "1"
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!