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

166
Visualizações
checking if a function with no return executed properly

I have simple function that aims to log a user in via localstorage, and then a second one that checks if the user can be found ( i need it separated because i need the logincheck elsewhere)

  logInUser(user: IPerson): Boolean {
    localStorage.setItem(this.userID, JSON.stringify(user));
    return this.isUserLoggedIn();
  }

 isUserLoggedIn(): Boolean {
    return Boolean(localStorage.getItem(this.userID));
  }

But this code seems bad to me because im not actually verifying that logInUser did its job, im just calling another function for this. I was thinking about converting this into a Promise of boolean and chain .then() but this doesnt work with setItem()

technically, setting something to a storage does not return anything, so i dont know how to verify correctly that it did and catch errors.

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

If it returns without exception you can assume it worked. If you really want you can double check it with localStorage.getItem(this.userID) === JSON.stringify(user) and that's the ultimate test.

Exception might occur if you try for example to setItem in a snippet in stack overflow.

about 4 years ago · Santiago Gelvez 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