0

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
1
  • 1
    Call them in .then handler.. Commented Jun 2, 2016 at 4:41

3 Answers 3

5

You need to make proper use of the .then() method:

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

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

    return object.function3();
}).then(function (result) {
    //result of petition $http of function3();
});
Sign up to request clarification or add additional context in comments.

Comments

0

You can try with this.

$http.get('URL1').success(function(data){
    $http.get('URL2').success(function(data){
        $http.get('URL3').success(function(data){
            console.log('Done');
        }
    }
});

Comments

-1

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();
        });
    });

});

1 Comment

You should avoid the "tower of doom": gist.github.com/sumtraveller/2a42b930dba2375c331e

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.