我有一些控制器功能可以調用服務功能(其中包含$ 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值,對嗎?
那麼,鏈接這些功能的「最佳實踐」是什麼?如果存在任何錯誤,還可以處理錯誤?請幫忙。
你可以做一個回調鏈,即調用getData2()getData1的回調()內。這是最簡單的方法。然而,對於更復雜的用途,您可以考慮使用諾言。 –
我不想使用回調鏈。它使getData2()調用依賴於getData1()。我想分開關注的問題,即從一箇中心位置控制這種鏈接。 – Rishabh
如果你不想使用回調,你可以嘗試使用承諾,或一些es6功能,如異步/等待 –