FlyingCat
FlyingCat

Reputation: 14288

How to handle multiple http requests in my case?

I have a question regarding the http request. I need to make multiple http requests and get the final result

My codes

var customer[];

var url = '/api/project/getCustomer';
    getProject(url)
        .then(function(data){
             var id = data.id 
             //other codes
             getCustomer(id) 
                 .then(function(customer) {
                     //other codes
                     customer.push(customer)
                  }


         }



var getProject = function(url) {
    return $http.get(url);
}

var getCustomer = function(id) {
    return $http.get('/api/project/getDetail' + id);
}

My codes work but it need to append multiple .then method in my codes and I was wondering if there is a better way to do this. Thanks a lot!

Upvotes: 0

Views: 57

Answers (1)

New Dev
New Dev

Reputation: 49610

There is a better way :)

getProject(url)
  .then(function(data){
     var id = data.id 
     //other codes
     return getCustomer(id);
  })
  .then(function(customer) {
     //other codes
     customer.push(customer)
  });

This works because .then returns a promise, so you could .then it in turn.

Upvotes: 2

Related Questions