简体   繁体   中英

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM