Does ngStorage perform operations asynchronously?

2019-05-13 08:25发布

问题:

I'm working with angular.js and ngstore. I'm using a token based authentication with a node.js REST service, from the angular app I send my credentials to rest server and I receive the token, I then store this token in $localStorage to use throughout all the angular app($localStorage.token). But it turns that sometimes $localStorage.token is undefined, even when I assigned the token to it, so when I call another rest endpoint sending the token in the headers, I'm actually sending an undefined value. Also when I try to logout I do

delete $localStorage.token

but when I check if the user has been loggedout actually the token still there. What is strange is that if I set breakpoints right after deleting the token or assigning the token and wait for a while, everything works, that's making me think that those operations may be asynchronous?

Is this a common problem? How could I fix this?

Any help would appreciated, Thanks.

EDIT: actually I found that the problem is when using window.location, if I use $location.path it's working, but for certain reasons I need to use window.location, and it should work as far as I know

回答1:

I had the same problem today, and using the following commit worked for me.

https://github.com/raynode/ngStorage

There is a helpful discussion about this problem here:

https://github.com/gsklee/ngStorage/issues/39

In this version of ngStorage, the author has thoughtfully provided a way to "save" before proceeding and performing a $window.location.reload();

During login:

$localStorage.value = 100;
$localStorage.$save();

(and then) $window.location.reload();

During logout:

delete $localStorage.value;
$localStorage.$save();
$window.location.reload();

This worked for me to ensure that the $localStorage variables were deleted before page reload.



回答2:

No, local storage is not asynchronous.

JavaScript is a single thread environment and read/write operations to local storage occurs immediately.

Both session and local storage containers are the same. Except that session storage is a key/value pair with an expire timestamp specified.

Storage in HTML5 is not a reliable resource. As there are a number of browser states that will restrict or remove storage.

You are referring to ngStore which is a module I've never heard of. There is no local storage module that is included with AngularJS by default, and there are multiple open source modules that handle this.

What is most likely happening is that you are handling a session token as a state variable instead of a state promise. When you use a variable to hold the state of a resource on the server, then that state has 3 possible values. 1 not assigned, 2 pending assignment and 3 value assigned.

Instead of reading the token from storage directly. You should have a service that returns a promise that will resolve to provide that token. This promise will only resolve after the REST operation has completed.