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

Cristian Fernando - Mar 24 '23 - - Dev Community

Explica este código JavaScript

Dificultad: Intermedio

const getName = (name) => {
  return new Promise ((resolve, reject) => {
    setTimeout(() => {
      resolve(name)
    },3000)
  })
}

const getAge = (age) => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      if (age < 18) reject(new Error(`${age} no es edad valida`))
    }, 2000)
  })
}

const counterFriends = (friends) => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(friends.length)
    }, 5000)
  })
}

const getPromises = () => {
  const arr = [
        getName("Pepe"), 
        getAge(15), 
        counterFriends(["Ana", "Roberto", "Juan"])];
  return Promise.any(arr)
    .then((response) => {
      console.log(response)
    })
    .catch(err => console.log(err))
}

getPromises() 
Enter fullscreen mode Exit fullscreen mode
  • A. 15 no es edad valida
  • B. 3
  • C. Pepe
  • D. Promise { <pending> }
  • E. ["Pepe", "15 no es edad valida", 3]

Respuesta en el primer comentario.


. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .