我用一些参数调用一个资源,一切都很好。但如果资源不存在(服务器已关闭),我的承诺似乎永远不会被解决/拒绝。代码如下所示:
//My service:
// ...
getFooListFromBackend = function(){
var deferred = $q.defer();
Restangular.all('foo')
.getList({
param1: 123})
.then(function(fooList){
//success... do something in service
deferred.resolve('foos loaded');
})
.catch(function(failure){
deferred.reject('Error loading Foos, connection to Server failed');
});
return deferred.promise;
}
// ...
//My Controller
// ...
getFooListFromBackend()
.then(function(){
//success... do something in controller
})
.catch(function(error){
//show error
});
// ...
如果服务器关闭,服务器和控制器都不会访问catch()函数。任何建议如何处理这个?
=======更新==========
感谢Bergi,我通过查看延迟反模式来实现它:
//My service:
// ...
getFooListFromBackend = function(){
return Restangular.all('foo')
.getList({
param1: 123})
.then(function(fooList){
//success... do something in service
})
}
// ...
//My Controller
// ...
getFooListFromBackend()
.then(function(){
//success... do something in controller
})
.catch(function(error){
//show error
});
// ...