hh
Posts: 0
Joined: Sat Feb 04, 2017 10:07 am

Code execution not sequential

I have code in init function on the init page which must be run at the startup but the code is not executed in sequence.

The problem is a call to a REST service that must do a status check and that call is completed after code that should be run after the call. That lead of course to errors...

Is it a bug or as designed that the javascript code will not run in sequence when mixed with REST calls?

Can also say that your code examples is not good and easy to follow. Especially google maps example is hard to follow because you mix at least three examples in one

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Code execution not sequential

Hello,

Could you clarify what you have tried and what exactly does not work?
If you run several services one-by-one - please run them in the success function of the previous service

hh
Posts: 0
Joined: Sat Feb 04, 2017 10:07 am

Code execution not sequential

index page

  • the built in init function {

    1) some expressions
    2) more expressions
    3) call to another function within the index page which in turn call a REST service to check status in db. The return value is set to a scope variable
    4) another expression that is dependent on the result from the REST call. This expression checks the scope variable from step 3

    The problem is that the response form step 3 comes after step 4 have been done.

    I'll see if I can change the code so I can place it in the success section

hh
Posts: 0
Joined: Sat Feb 04, 2017 10:07 am

Code execution not sequential

Now I have tried another way of doing it byt the problem moves to another place.
Depending on the status, I want to show different start pages but because of the issue above, (REST call is not done sequential) the wrong pages first shows up before the redirection to the correct page. The page that is shown is the page that are set as default for the app. If I hardcode the value there is no problem to redirect to the right page.

Feels to me like a bug but i don't have enough insight to know if I'm correct about that. Not good I one can't rely on the execution order....

hh
Posts: 0
Joined: Sat Feb 04, 2017 10:07 am

Code execution not sequential

I think that there are a bug regarding the navigate to or similar.

from the beginning I wanted to do some checks in the init method of index page and then redirect to another page than the default page set for the app.
Even though all code was in the init method it didn't run in sequence as I have tried to explain above

Now I have moved some code from index-init to the success section in the method that calls the rest service and after the rest-call, I have the navigate to - expression.

What happens is that the navigate-to is executed before the console.log message n this snippet

// METHOD isCheckedIn in index which calls the REST service

...
// Success section of the REST call in isCheckedIn
$scope.isCheckedIn = isCheckedIn_scope;
console.log("Index:ischeckedin - after db access =" + isCheckedIn_scope);

Code: Select all

     $scope.setCheckInStatus(isCheckedIn_scope); // METHOD IN INDEX 
     if(isCheckedIn_scope) { 
          Apperyio.navigateTo("Activities", {}); 
     } 

...

From the log which shows that for some reason the functions in activities is run before the functions in index even though the calls comes after those
[Log] activities::init - inside if (ActivitiesController.js, line 23)
[Log] Activitis::getAllSessionsByArea (ActivitiesController.js, line 33)
[Log] Index:ischeckedin - after db access =true (indexController.js, line 65)
[Log] Index::setCheckInStatus (indexController.js, line 180)

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Code execution not sequential

Please use promises to make requests async: https://docs.angularjs.org/api/ng/ser...

It seems the app navigates to to the previous page, because you use 2 navigation:
1) On the success of the REST request
2) On the next line of that REST request

Please use only one here. If you need to run any JS code right after the service navigation - please add it to the init function on that next page

Return to “Issues”