Local Storage not instantly available

I store an authentication token in a localStorage variable window.localStorage.getItem("token");

I query this variable in the `$ionicPlatform.ready(function() { }); and if I don’t set a time out of 1500, the variable isn’t found

if(window.localStorage.getItem("token") != null){
    // Never true

    if(window.localStorage.getItem("token") != null){
        // True

I’m not sure what the “proper” way is to get around this, is there a “local storage ready” broadcast event? I’m not storing anything that complex in local storage (a few arrays), so I don’t believe that is causing the hold up.

Any pointers?


I’m looking to solve a similar problem.
I guess what we are looking for is some way to “halt” the app to continue loading until the localstorage data has been set?

My problem is that I add content from a .json file that contains several thousand objects…

Have you wrapped it in a ‘device ready’ function?

Yepp… I have it like this right now:

.run(function($ionicPlatform, $localForage, $http) {
  $ionicPlatform.ready(function() {
    // Get names and save in localStorage
    $http.get('js/names.json').then(function(names) {
      $localForage.setItem('names', names.data).then(function(data) {
        console.log('Store all names in localStorage');
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {

    if (window.StatusBar) {
      // org.apache.cordova.statusbar required

I have found differences between device ready and ionic ready. It may be worth specifically trying device ready.

In addition, I don’t know if the issue is that you’re not getting the names from the JSON file in time. Would you not need a promise on that so that it try to do the setItem until it has completed the http.get?

Have you console logged between the get and the set to check that the file is fully read in before it tries to set them into local storage?

Interesting, I had assumed that ionic.ready would have occurred after device.ready, I’ll give this a go, thanks!

In my console I see that the directive is calling the get function before the set is called…?
Should that be the case when I have the set function in .run block?

That may well be the case. I do know we have had some oddity with this in the past, but that may have been related to an incorrect implementation on our side or something with old betas etc. Worth a shot regardless though until someone else can come along and maybe offer some more help.

Well waiting for the “deviceready” event seems to have done the trick! So thanks @fox !

What’s even more amazing is Apple took less than 3 hours to approve my update. That’s madness!

Great news on both counts.

Hopefully one of the main ionic team or someone else with a better understanding will be able to explain why device ready reacts differently to ionic ready. I’ve spent so much time on this and hundreds of other issues over the last year that I have forgotten the specifics. :slight_smile:

Anyway, glad I could help.