簡體   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