yavg yavg - 5 months ago 12
AngularJS Question

Put an order to the result of $http requests in Angular.js

I have 3 web requests that do through $http, this petition are in functions (

function1()
,
function2()
,
function3()
) . I would like to customize the order in which all these requests are executed.

object.function1().then(function() {
//result of petition $http of function1();
});

object.function2().then(function() {
//result of petition $http of function1();
});

object.function3().then(function() {
//result of petition $http of function2();
});


they all try to run at the same time. some requests take longer than others because they get more JSON objects. I want to run in order to start by:

function1(); //first
function2(); //second
function3(); //three

Answer

Call the functions inside the other function's callback like this:

object.function1().then(function() {
    //result of petition $http of function1();

    object.function2().then(function() {
        //result of petition $http of function1();

        object.function3().then(function() {
            //result of petition $http of function2();
        });
    });

});