2014-11-03 69 views
0

我有一个关于http请求的问题。我需要多个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方法在我的代码,我想知道如果有一个更好的办法做这个。非常感谢!

回答

2

有一个更好的办法:)

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

这工作,因为.then返回一个承诺,所以你可以反过来.then它。

相关问题