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

241
Views
Uso adecuado de Async JS para garantizar que las llamadas a funciones esperen a que las funciones anteriores se completen (¿resuelvan?)

Tratar de aprender JavaScript async/await adecuado para ejecutar funciones en secuencia cuando una función temprana en la secuencia se retrasaría (usando setTimeout para simular). No obtengo los resultados esperados (todavía obtengo "primero", "segundo", "¿esto debería ejecutarse primero?" - Ver código).

¿Qué me estoy perdiendo? ¿Tengo una idea equivocada sobre esto?

¡Gracias por adelantado!

 const zeroFunction = () => { setTimeout(() => { return new Promise((resolve) => { console.log("This should run first?"); resolve(); }); }, 2000)} const firstFunction = () => { return new Promise((resolve) => { console.log("first"); resolve(); }) } const secondFunction = () => { return new Promise((resolve) => { console.log("second"); resolve(); }) } async function fnAsync() { await zeroFunction(); await firstFunction(); secondFunction(); } fnAsync();
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

zeroFunction actualmente está devolviendo undefined implícitamente, no una Promesa. Invierta el ajuste del constructor setTimeout y Promise y debería funcionar como se esperaba.

 const zeroFunction = () => { return new Promise((resolve) => { setTimeout(() => { console.log("This should run first?") resolve() }, 2000) }) } const firstFunction = () => { return new Promise((resolve) => { console.log("first") resolve() }) } const secondFunction = () => { return new Promise((resolve) => { console.log("second") resolve() }) } async function fnAsync() { await zeroFunction() await firstFunction() secondFunction() } fnAsync()

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!