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

90
Views
varias esperas se ejecutan en paralelo

Estoy tratando de aprender async/await y las promesas y no puedo ejecutar varias funciones asíncronas en serie.

 function firstFunction () { return new Promise((resolve, reject) => { setTimeout(() => console.log('first'), 2000) resolve() }) } function secondFunction () { return new Promise((resolve, reject) => { setTimeout(() => console.log('second'), 1000) resolve() }) } async function main() { await firstFunction() await secondFunction() } main()

La salida que obtengo es

 second first

Dado que hay una espera para firstFunction(), esperaría que la promesa de firstFunction() termine antes de pasar a secondFunction().

¿Alguien puede señalar por qué se ejecutaron en paralelo y cómo hacer que se ejecuten uno tras otro? (se ejecutó en el nodo v18.3.0)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Debe mover su llamada resolve( ) dentro de la función setTimeout() . Aquí está el código fijo:

 function firstFunction () { return new Promise((resolve, reject) => { setTimeout(() => { console.log('first'); resolve(); }, 2000); }); } function secondFunction () { return new Promise((resolve, reject) => { setTimeout(() => { console.log('second'); resolve(); }, 1000); }) } async function main() { await firstFunction() await secondFunction() } main()

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!