SANDEEP PANDA
SANDEEP PANDA

Reputation: 31

Synchronous in angular controller

In the following code, the controller is not invoking the third $http.get() call and instead goes directly goes to the end.

I want to execute all $http.get() requests. In my code, the third $http.get() depends on the second $http.get() result. Additionally, the second $http.get() depends on the first $http.get() result.

Code

Does anyone have an idea why the third $http.get() is not being invoked?

Upvotes: 1

Views: 211

Answers (2)

Ramesh Rajendran
Ramesh Rajendran

Reputation: 38663

Suggestion:-

Actually this is a big issue with angular http call, Because http call does not support async :false

See my answer in this discussion : Angularjs $http VS jquery $.ajax

Your code look like a lot of confusions for call new http method inside of inside methods.

And your code looks like not standard format also gives lot of confusion.

So if you change any code in this file after some day's, then you need to put a microscope glass in your eye, and will see one by one lines. it's will take more times. So avoid http call only for this type of situation.

Solution:-

Avoid http call means, pleas do with Ajax call with async:false option

The code look like

$.ajax({
    type:       "GET",
    dataType:   "Json",
    url:        'Your URL',
    **async:      false,**

    success: function (returndata, status, jqxhr) {
        $(this).html(returndata).hide().fadeIn();
    }).fail(function() { 
        alert("error"); 
    })
});

Explanation About Async:false

Setting async to false means that the statement you are calling has to complete before the next statement in your function can be called. If you set async: true then that statement will begin it's execution and the next statement will be called regardless of whether the async statement has completed yet.

Upvotes: 0

hisabimbola
hisabimbola

Reputation: 306

with the way you structured your code, this code below will run before the third $http.get

orderDetails['orderData'] = data;
orderDetails['kitNames'] = kitNames;
orderDetails['proteins'] = proteins;
orderDetails['dietaries'] = dietaries;
orderDetails['ingredients'] = ingredients;

you would have to put it in the third $http.get and every other code that depends on it, and that should solve your challenge

Also, your code can be refactored, so it is more readable and understandable for you to read

Upvotes: 1

Related Questions