[英]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.