我目前正在嘗試編寫某種配置服務。該服務提供了到另一臺服務器的路徑。該路徑將通過使用接收路徑的ping功能進行有效性測試。如果路徑指向一個不存在的服務器,則不會有任何承諾,並且整個應用程序由於在代碼中使用.then(function (data){ });
交付承諾後通常應發生的操作而受阻。
那麼有沒有辦法如何捕獲這樣的失蹤/未決的承諾,以防止凍結我的應用程序?AngularJS catch理想承諾
這裏是在AngularController代碼(得到由按鈕觸發)
applicationPingService.ping(path)
.then(function(successStatus){
if(successStatus == true){
//Some code here
}
else{
//Some other code here
}
});
這就是所謂的服務。
它需要一個像http://localhost:8080
這樣的路徑,並嘗試到達blank.html
頁面來檢查服務器啓動與否時的狀態碼。
app.service('applicationPingService', ['$http', '$q', 'globalValue', function($http, $q, globalValue) {
this.ping = function(path)
{
return $http({
method: "GET",
url: path + "/blank.html"
}).then(function(response)
{
if(response.status == 200)
{
return true;
}
else{
return false;
}
});
};
}]);
這只是一個例子,但我也想知道一個普遍的方式如何捕捉空轉和失蹤承諾
在此先感謝