RedBottleSanitizer
RedBottleSanitizer

Reputation: 93

AngularJS function execution sequence question

I am using calling function Save_Click in Angular controller on button click.

Following is the sequence of execution

Line Code 
1    Declare a variable V1 
2    console.log(V1) 
3    Perform a server post back, and assign a value to V1 depending on the values the service returns. console.log(V1)
4 console.log(V1) 

What I see is that - in console #4 is printed before - #3 Also the value assigned in #3 is not printed in #4.

What could be the reason? What I am doing wrong? I get the same issue if I use $localStorage (ngStorage) library too.

Note - I am using Visual Studio/.Net solution to host the angular application in my Index.cshtml.

Upvotes: 0

Views: 43

Answers (2)

RedBottleSanitizer
RedBottleSanitizer

Reputation: 93

Finally did the trick for me.



var x = {};
            
                    x.1 = '1';
                    x.2 = '2';
                   
                    MyService.CallAPI('RunRabbit', x).success(function (data) {
                        if (data === null) {
                            $scope.error = true;
                            $scope.errorDescription = "No data found for selected criteria.";
                            
                        } else {
                           
                            $scope.answer = data.answer;
                           
                        }
                    }).error(function () {

                    }).finally(function () {
                        console.log($scope.answer);
                    });

I could evaluate $scope.answer and then use it in synchronous way from finally.

Upvotes: 0

Jijo Cleetus
Jijo Cleetus

Reputation: 2867

Use below method

1    Declare a variable V1 
2    console.log(V1) 
3    async (Perform a server post back, and assign a value to V1 depending on the values the service returns. await console.log(V1))
4    console.log(V1) 

Now #3 will prints before #4

Upvotes: 1

Related Questions