我对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进行。

07-26 09:29
查看更多