繁体   English   中英

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

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

我对http请求有疑问。 我需要发出多个http请求并获得最终结果

我的密码

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

我的代码可以工作,但是需要在我的代码中附加多个.then方法,我想知道是否有更好的方法可以做到这一点。 非常感谢!

有一个更好的方法 :)

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

之所以.then是因为.then返回一个承诺,因此您可以依次.then

暂无
暂无

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

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