verifying the successful execution of a void function

I have a straightforward function that is designed to log in a user via local storage. I also have a second function that checks if the user can be found (I need them separated because I require the login check elsewhere).

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

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

However, I feel like there may be issues with this code because I am not actually confirming that logInUser successfully completed its task; instead, I am just calling another function for verification. I have considered converting this into a Promise of boolean and chaining .then() but unfortunately, this approach does not work with setItem().

In theory, when setting something to local storage it does not return anything, so I am unsure how to properly verify that it was successful and handle any potential errors.

Answer №1

When the function returns without any errors, you can be confident that it was successful. For added assurance, you may verify by using

localStorage.getItem(this.userID) === JSON.stringify(user)
, which serves as the ultimate test.

An exception could arise if attempting to use the setItem method within a code snippet on Stack Overflow.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue regarding the slash format in credit card expiration dates

When entering the credit card expiration date, I am currently facing an issue. Every time I input two numbers, I need to manually add a slash (/) after them. However, if I try to delete the third number, it only removes one character. Is there a way to mak ...

Exploring the intricacies of Bower and enhancing dependency management

Currently, I am working on developing a project named myapp using angularJS along with Yeoman Generator. This project involves utilizing Bower to manage dependencies and Grunt to wiredep those dependencies into the index.html file (which essentially genera ...

Refactor the fat arrow function in Typescript to maintain the bare function signature verification

When using AOT in Angular, it is necessary to rewrite all functions and reducers to not utilize arrow functions: An error occurred: Error encountered resolving symbol values statically. Function calls are not supported. Consider replacing the function o ...

show the content depending on the result given by a connected class to a function

Currently, I am executing a loop where each item contains a button with a specific class that is linked to a method. The text displayed on these buttons depends on the value returned by the mentioned method. HTML Template <button v-for="(item, ind ...

Having trouble with Angular redirecting to the home page?

Having a bit of trouble: core.es5.js:1020 ERROR Error: Uncaught (in promise): Error: Unable to match routes. URL Segment: 'home' This is the code snippet from view.html: <div class="container"> This serves as the main app; <a rou ...

Difficulty encountered while implementing Ajax POST in CodeIgniter

I've been working on a project similar to a ticket system that occasionally requires lengthy answers. When using CKEDITOR in the answer area, the agent's changes are automatically saved to the database using Json GET. However, I encountered an er ...

When utilizing "ng2-file-upload" in conjunction with Angular 2 and Typescript, encountering a limitation where files larger than 1MB cannot be uploaded

My attempt to upload a file with a size exceeding 1MB is triggering an error regarding its large size. Despite setting the limit to 50 MB, it doesn't seem to be working as expected. Can someone please assist me in figuring out what I am doing incorrec ...

What's the significance of & in TypeScript and JavaScript?

While exploring someone else's code, I came across this interesting piece related to the props of a React component. Although I'm aware that & is typically used as an AND logical operator, it seems to have a different significance in this con ...

The solution to the issue: [splide] Unable to find a track/list element

Currently, I am trying to create a thumbnail carousel following this tutorial, but I seem to be encountering an error. The guide does not provide instructions on how to create the #main-carousel element, so despite trying different methods, I am still unab ...

Error in Node.js with MongoDB: Array of OptionalId<Document> Typescript typings

I have successfully established a connection and written to my MongoDB collection, but I am encountering a type error that is causing some confusion. Below is the code snippet along with the error message: interface Movie { id: number; title: string; ...

Using jQuery to implement AJAX file uploads

Currently, I am facing a challenge. I have multiple forms on a single page that are being submitted to the backend asynchronously via ajax. Some of these forms require a file upload without interrupting the process, so it needs to be handled asynchronousl ...

Guide on positioning a span element to the left using the margin auto property in CSS for Angular 4

Having trouble with moving numbers highlighted to the left with names in CSS. I've tried using flex direction and margin auto but can't achieve the desired result. https://i.sstatic.net/kRJOb.png Here is my HTML code: <section class="favorit ...

Eliminating Non-Breaking Spaces with the Click of a Button

Having some difficulty with the following Javascript code. I'm not very skilled in writing Javascript, so any assistance on adjusting it to replace any &nbsp; with a regular space would be greatly appreciated. Thank you function copyToClipboard( ...

Union template literal types are preprended in Typescript

Is there a method to generate specific string types from existing string types with a designated prefix? It's better to acknowledge that it doesn't exist than to dwell on this concept. type UserField = "id" | "name"; type Post ...

The Content-Type header is missing in the Ajax request for json

In my PHP code, I generate valid JSON and set the content-type header to application/json in my development environment. However, when I deploy this code to an embedded web server, it functions properly but is unable to send the appropriate content-type he ...

What could be causing the React component to not render?

I am currently developing a React application and I need to place an order. To do so, I require a comprehensive list of all users and products available. The CreateOrder component is integral to this process as it contains two crucial variables in its stat ...

Utilize JQuery to implement fading effects for clicked elements in a webpage

I've been using a rollover JavaScript plugin to create smooth transitional effects when users hover over clickable page elements. Everything was going well until I decided to switch to making ajax calls instead of page loads for dynamic content. The p ...

ng-repeat closing function event

Looking to create a callback method that gets called every time ng-repeat is triggered. After doing some research, I found a couple of options: 1. Using ng-init with ng-repeat This approach involves calling the method using the ng-init property: <ol ...

C# - Issue with Webbrowser failing to fully load pages

I am facing an issue with loading pages completely on the web browser, likely due to heavy usage of JavaScript. To address this problem, I have integrated another browser into the project called Awesomium. I am wondering if Awesomium supports using getEle ...

Using three.js to establish an Image for Particle

I am looking to make some changes to this demo: Instead of having colored particles, I want to assign an image to each one. Should I use a cube for this purpose? Or is there a way to use an image with Vector3? Here is the code for the example: i ...