2016-10-21 38 views
0

我有一些控制器功能可以調用服務功能(其中包含$ http請求)的不同任務。例如:連鎖控制器的功能/代碼的順序和處理錯誤

// this should be run first 
function getData1(request) { 
    dataService.getData1(request).then(function success(response) { 
     return response; 
    }) 
}; 

// this should run after getData1() 
function getData2(request) { 
    dataService.getData2(request).then(function success(response) { 
     return response; 
    }) 
}; 

我呼籲某一事件從一個集中的地方,這些功能(比如,當用戶改變輸入/請求數據):

$scope.loading = true; 
$scope.$watch('input', function(newValue, oldValue) { 
    if(newValue !== oldValue) { 
     request = newValue; 
     $scope.data1 = getData1(request); 
     $scope.data2 = getData2(request); 
     $scope.loading = false; 
    } 
}); 

但據我所知, getData1()getData2()發出異步請求,並且在數據加載之前可能會導致$scope.loading = false

另外,如果一個或多個請求中有任何錯誤,則$scope.data1$scope.data2變量將具有被拒絕的Promise值,對嗎?

那麼,鏈接這些功能的「最佳實踐」是什麼?如果存在任何錯誤,還可以處理錯誤?請幫忙。

+0

你可以做一個回調鏈,即調用getData2()getData1的回調()內。這是最簡單的方法。然而,對於更復雜的用途,您可以考慮使用諾言。 –

+0

我不想使用回調鏈。它使getData2()調用依賴於getData1()。我想分開關注的問題,即從一箇中心位置控制這種鏈接。 – Rishabh

+0

如果你不想使用回調,你可以嘗試使用承諾,或一些es6功能,如異步/等待 –

回答

2

getData1 and getData2(它又調用$http)返回promise

var promise1 = dataService.getData1(request); 
var promise2 = dataService.getData2(request); 

您可以使用$q.all()。通過promise1promise2$q.all。這並不能保證承諾將按順序解決。

If one promise fails, the combined promise fails. 
If all promises succeed, the combined promise succeeds. 

example link for $q.all

對於順序執行,

dataService.getData1(request1).then(function(response1) { 
    //do your stuff 
    return dataService.getData2(request2); 
}).then(function(response2) { 
    //response2 - response of dataService.getData2(request2) 
    //do your stuff 
}).catch(function(error) { 
    //on error if any of this fails. 
}) 
+0

我沒有從getData1()返回承諾。爲了返回一個承諾,我必須'返回dataService.getData1(request).then(函數成功(響應){返回響應; })'。 – Rishabh

+0

但我需要依次調用控制器函數getData1()和getData2()。因爲在這些調用之間還有其他任務需要處理。我想把數據讀取操作放在單獨的函數中,這樣我也可以很容易地對它們進行單元測試。把它們放在變量'promise1'和'promise2'中並不能解決連續調用的問題。 – Rishabh