Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

243
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda