2014-11-03 66 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它。

相關問題