Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

238
Vistas
If statement considers FALSE promise as TRUE

I'm making authentication in my app. And have such code

  const ttt = currentUser.changedPasswordAfter(decoded.iat);
  console.log(ttt);
  if (ttt) {
    console.log('if thinks ttt is true');

changedPasswordAfter retuns promise (true or false) So I run request and get this into console.

Promise { false }
if thinks ttt is true

As you see ttt is FALSE but IF statement decided that it is TRUE . How can I fix that ?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Promise is an object - so, it is "truthy". Only the resolved value of the promise can be true or false. To give the promise a chance to resolve, either the await or the Promise.then method should be used.

const ttt = currentUser.changedPasswordAfter(decoded.iat);
ttt.then((result) => {
   if (result)
     console.log('Promise returned true')
   else
     console.log('Promise returned false')
}) 
about 4 years ago · Juan Pablo Isaza Denunciar

0

Because ttt (which is a very bad name for a variable) is a Promise not a boolean so the if statement return True because the variable ttt has reference (is not undefined or null). try to add await keyword. it will work but you have to make the function Async

const ttt = await currentUser.changedPasswordAfter(decoded.iat);
  console.log(ttt);
  if (ttt) {
    console.log('if thinks ttt is true');
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda