Who can help me understand these two lines of code

source:

// decide which menu items should be hidden by current login status stored in local storage
this.userData.hasLoggedIn().then((hasLoggedIn) => {
this.enableMenu(hasLoggedIn === true);
});

this.enableMenu(true);

I don’t understand why after checking if user has loggedin, set the menu(loggedin menu) to ‘enable’ anyway

Not “anyway”, if user is not logged in, then the specific menu “loggedOutMenu” gonna be use respectively I guess that the menu doesn’t contains the same list of actions if the user is logged in or not

this.userData.hasLoggedIn().then((hasLoggedIn) => {
  this.enableMenu(hasLoggedIn === true); // 2. This comes after, if user not logged in => use menu "loggedOutMenu" else use menu "loggedInMenu"
});

this.enableMenu(true); // 1. This code first, use menu "loggedInMenu"

Does that answer your question or do I don’t get something?

ah, you mean, because it’s async, so
this.enableMenu(true) actually runs first?

yes exactly, if I understand correctly :wink:

this.enableMenu(true) will most probably run first because hasLoggedIn() is async with the use of a promise and will most probably be resolved afterwards

thx. I think that’s the case.

However I tried to do it like this in my app but didn’t work. Can you also give me some advise how I should troubleshoot?

Sure ok I follow. Mark this one as solved if it’s ok for you.

Just a side note, would be cool, when you post code to format it

this.userData.hasLoggedIn().then((hasLoggedIn) => {
    this.enableMenu(hasLoggedIn === true);
});

is easier to read than

this.userData.hasLoggedIn().then((hasLoggedIn) => {
this.enableMenu(hasLoggedIn === true);
});

they need to wrap their code in the triple backtick… if not, any leading space or tabs are ignored…