DEV Community

Cristian Fernando
Cristian Fernando

Posted on

2

Paracetamol.js💊| #204: Explica este código JavaScript

Explica este código JavaScript

Dificultad: Intermedio

const one = () => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      reject("one")
    }, 5000)
  })
}

const two = () => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      reject("two")
    }, 2000)
  })
}

const three = () => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      reject("three")
    }, 10000)
  })
}

const res = () => {
  return Promise.all([one(), three(), two()])
}

res()
  .then(x => console.log(x))
  .catch(err => console.log(err))
Enter fullscreen mode Exit fullscreen mode
  • A. "["one", "three", "two"]" (después de 10s)
  • B. "one" (después de 5s)
  • C. "three" (después de 10s)
  • D. "two" (después de 2s)

Respuesta en el primer comentario.


Top comments (1)

Collapse
 
duxtech profile image
Cristian Fernando

Respuesta:

  • D. "two" (después de 2s)

Cuando usamos Promise.all() basta que una promesa falle para que todo el proceso termine, en este caso curiosamente fallan todas las promesas, entonces evaluamos cual es la promesa que falla primero, osea la que tiene el delay mas breve, de las tres promesas two es la que falla primero, y por ende es el error que veremos por pantalla ignorando las otras promesas.

Image of Timescale

PostgreSQL for Agentic AI — Build Autonomous Apps on One Stack ☝️

pgai turns PostgreSQL into an AI-native database for building RAG pipelines and intelligent agents. Run vector search, embeddings, and LLMs—all in SQL

Build Today

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay