Statement execution Flow in Javascript function

Function execution priority in javascript.
Below I posted my code. Code functionality working fine but I am not sure about statement execution flow.
so after executing isValiduser() i am able to get role id but only when i write that to two different function and call one by one. is there any way i can execute function in the same order as I placed.
I know my question is quite confusing but please reply for more info.
I want step 1 execute first then step 2

exports.onTap = function()
{
  login.isValiduser(uname.text,passwordtext.text,40); // --- 1
  var scid = login.get("roleId").getItem(0); // ------------ 2
  console.log("role id: " + scid);
  if(parseInt(scid) ==1)
  {
    sc.load(parseInt(scid),95); 
    console.log("status: "+ sc.get("Status").getItem(0));
    if(sc.get("Status").getItem(0)==1)
    {
      appSettings.setBoolean("logedIn",true);
      var topmost = frameModule.topmost();
      topmost.navigate({ moduleName: "views/home/home",context:dockReceive});
    }
    else
    {
      dialogsModule.alert("Access_Denied");
    }  
  }
  else
  {
    dialogsModule.alert("Enter Valid Username and Password");
  }
}

I think you might need to clarify a bit, as I’m not following your question. Aside from the one function shown, which others are you referring to?

One way to ensure the sequence would be to wrap the call in a conditional, though I have a feeling maybe some kind of a callback would be more elegant. For example, if I had

function foo() {
  // do stuff
}
function bar() {
  // do other stuff
}
function baz() {
  foo();
  bar();
}

and I wanted to be absolutely certain that foo ran before bar I could do something like

function foo() {
  // do stuff
  return true;
}
function baz() {
  if ( foo() ) { 
    bar();
  }
}

if I call 3 different functions as step 1 as first function step 2 as second and third one start where i wrote sc.load();
but all three i need to handle in 3 different button click then it’s working by but not as I was trying above.

If logic structures alone do not give you enough control a callback should work. IMHO it’s a little bit beyond newbie level, but is something well worth learning because they come in useful often. Basically it’s a way to pass a function to a function where the code runs synchronistically

Thank you

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.