Wait for Local Storage [SOLVED]

In my App i need to check, if a user is allready in the local storage, befor i call my api. if the user is allready in the storage, i want to give back the old data and dont call the api again.

My problem is, that when i try to check the sotrage, my app calls the api to fast. I dondt understand why.

So i call my getUser methode, in this method i check the localstorage value and then i check the value.

Maybe there is a way to tait for the local storage data.

1 Like

You need to use the Promise that is returned by the Storage.

    this.storage.get(key).then((value) => {
        this.alreadyHasThe(value);
    }).catch((empty) => {
        this.checkValue(empty);
    });

Yes i do this, but always the api is called, and the storage output comes later…

callApi(){
if(checkUser()){
return new Promise oldData;
} else {
 return new Probime (api call blabla);
}
}`

So i alwasy get a false, and after the api is called, on the console i get the storage output wo is true…

callApi() {
    return new Promise((resolve, reject) => {
        if (user = checkUser()) {
            resolve(user);
        } else {
            reject(false);
        }
    });
}

And on the receiving side (if it is split up in multiple functions): class.callApi().then(result => { /process/}).catch(no_result => {/* fetch*/});

You could also just call the function to fetch the user here in callApi() and resolve with new fetched data. (instead of rejecting it and handling it elsewhere.)

checkUser(tag) {
    this.local.get('oldUser').then((result) => {
        console.log(result);
        if (result == tag) {
            return true;
        } else {
            this.local.set('oldUser', tag);
            return false;
        }
    });
}

And then

loadProfile(platform, region, tag) {
    
    if(this.checkUser(tag)){
        return Promise.resolve(this.profileData);
    } else {
        return new Promise(resolve => {
            this.http.get(this.api + platform + '/' + region + '/' + tag + '/profile')
                .map(res => res.json())
                .subscribe(data => {
                    this.profileData = data;
                    resolve(this.profileData);
                });
        });
    }

};

I can see the problem. The http uses Observable. This is executed asyncronously.
You should use callback functions in your loadProfile, I suppose… or wait!
Maybe try this using ‘toPromise()’ and modified your code to return promise:

Can you explain me/change my code to do this?
Would be realy helpful :slight_smile:

checkUser(tag) {
    return new Promise((resolve, reject) => {
        this.local.get('oldUser').then((result) => {
            console.log(result);
            if (result == tag) {
                resolve(true); 
            } else {
                this.local.set('oldUser', tag);
                reject(false); //thus we can use catch
            }
        });
    });
}

loadProfile(platform, region, tag) {

    this.checkUser(tag).then(() => {
        return Promise.resolve(this.profileData); //why even return it?
    }).catch(() => {
        return this.http.get(this.api + platform + '/' + region + '/' + tag + '/profile')
            .map(res => res.json())
            .toPromise().then((data) => { //toPromise; might be the key to succes here!!
                this.profileData = data;
                return Promise.resolve(this.profileData); //why even return it?
            });
    });

};

let me know… i also added some comments because, maybe you do not need to return the promise in loadProfile? Because then the work is done, right?

Now i have the problem in my home.ts

“Error TS2339: Property ‘then’ does not exist on type ‘void’”

Oh, then you forgot to return the Promise!

loadProfile(platform, region, tag) {
    return .......

Now i recive only undefined values

I fixed it wit an sqlite solution.

Hello LolStats,

How you fixed your issue. I have the same prolem, i want to get user information in localstorage to load in form.

Please can you help ?

Regards,

I hope this link will help you