Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!