DEV Community

Cristian Fernando
Cristian Fernando

Posted on

 

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

Explica este código JavaScript

Dificultad: Intermedio

const randomNumber = () => {
  return new Promise((resolve, reject) => {
    let random = Math.floor(Math.random() * 10) + 1;
    if(random >= 5){
      resolve(`Bien... ${random}`)
    }else{
      reject(`Mal... ${random}`)
    }
  })
}

randomNumber()
  .then(x => console.log(x))
  .catch((error) => console.log(error))
Enter fullscreen mode Exit fullscreen mode

A. Dependiendo del valor de random imprimirá Bien o Mal
B. Promise { <pending> }
C. Promise { <fulfill> }
D. Ninguna de la anteriores

Respuesta en el primer comentario.


Top comments (1)

Collapse
 
duxtech profile image
Cristian Fernando

Respuesta:
A. Dependiendo del valor de random imprimirá Bien o Mal

En este ejemplo el estado de la promesa cambiará en función del valor de la variable random, cuando cambie este valor la promesa se volverá a evaluar y podrá ser satisfecha o no.

The JavaScript Brief

1. Top 5 MERN STACK projects to improve your practical understanding

Boost your MERN Stack development skills by undertaking interesting beginner projects. These five engaging projects cover web applications and range from social media website applications to geo-social networking maps. Hone your understanding and apply modern techniques backed up by hands-on experience.

2. How To Optimize Your React App’s Performance

Learn the best optimizing techniques to make your React applications faster and more efficient. Focusing on the identification of performance bottlenecks and common pitfalls to avoid, these optimization strategies will keep your applications running smoothly even when faced with growing complexity.

3. A story of let, const, object mutation, and a bug in my code

In the pursuit of bug-free code, explore an incident involving a mix-up between const and let, making sure your custom code works effectively with third

party documentation. Discover best practices on program flow and learn about JavaScript's unpredictable aspects to ensure your core code is robust.