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

161
Views
Cómo crear tareas asíncronas en JavaScript

Cómo hacer tareas asincrónicas como esta:

Quiero tener dos tareas: una imprimiría "foo" después de 1 segundo y la otra imprimiría "barra" después de 2 segundos. Quiero que se inicien al mismo tiempo y se ejecuten de forma asíncrona.

Aquí hay un ejemplo de esto en python:

 async def main(): task1 = asyncio.create_task( say_after(1, 'hello')) task2 = asyncio.create_task( say_after(2, 'world')) print(f"started at {time.strftime('%X')}") # Wait until both tasks are completed (should take # around 2 seconds.) await task1 await task2 print(f"finished at {time.strftime('%X')}")

¿Cómo hago algo como esto en JavaScript? ¿Uso promesas? No sé cómo funcionan. Soy "nuevo" en javascript

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

0

Puede usar then método de promesas js.

 function timeout(time_ms) { return new Promise(resolve => setTimeout(resolve, time_ms)); } function main() { timeout(1000).then(() => console.log("hello")) timeout(2000).then(() => console.log("world")) var d = new Date(); var n = d.toLocaleTimeString(); console.log(`started at ${n}`) } main()

about 4 years ago · Juan Pablo Isaza Report

0

Le recomiendo encarecidamente que no use async pero prometa si quiere ser bueno en JavaScript.

Promise es una expresión que tiene una fuerte estructura algebraica y async-await, por otro lado, son declaraciones que tienen reglas complicadas.

about 4 years ago · Juan Pablo Isaza Report

0

¡Hola!

Puedes hacerlo así:

 async function hello() { setTimeout(() => {}, 1000) console.log("hello") } async function world() { setTimeout(() => {}, 2000) console.log("world") } async function main() { await hello() await world() } main()

¡Déjame saber si eso funciona para ti!

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!