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.
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.