How does one chain of sequential / sequential $ http messages in angular?

I am new to AngularJS and am learning as I go. How can I order sequential $ http messages? I need the response data from the first $ http POST to use in the second $ POST POST, from which I also need the response that this second POST returns.

    method: 'POST',
    url: '',
    data: '{"field_1": "foo", "field_2": "bar"}',
    headers: {'Content-Type': 'application/json'}
}).then(function(resp) {

   $scope.data_needed = resp.data_needed;
    // Can't possibly do another $http post here using the data I need, AND get its reponse?
    // Would lead to a nested relationship, instead of adjacent chaining.

}, function(err) {
    // Handle error here.


I found out that for the same reason, it is not possible to link another $ http post from the last line of code to another .then(function(resp) {});

(referring to the 1st comment in the code block above).

Any advice? All I can find are examples of $ http GET chaining that are not related to getting and using the response. Greetings.


source to share

1 answer

This is the way:

        function success1(response) {
            var data =;
            $ =;
            return $http({...});
        function error1(response) {
            return $q.reject(response);
        function success2(response) {
            var data =;
            $scope.yyy = data.yyy;
        function error2(response) {
            // handle error


When the function then()

returns a promise (part return $http(...)

), the chain then()

is called with the resolved value of the second promise. Also note the part return $q.reject(...)

required to jump to the second error function instead of the second success function.



All Articles