简体   繁体   English

在我的情况下,如何处理多个http请求?

[英]How to handle multiple http requests in my case?

I have a question regarding the http request. 我对http请求有疑问。 I need to make multiple http requests and get the final result 我需要发出多个http请求并获得最终结果

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. 我的代码可以工作,但是需要在我的代码中附加多个.then方法,我想知道是否有更好的方法可以做到这一点。 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. 之所以.then是因为.then返回一个承诺,因此您可以依次.then

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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