Restangular Promise在服务器脱机时从未解决过?

时间:2015-02-27 11:14:16

标签: angularjs promise restangular angular-promise

我用一些参数调用一个资源,一切都很好。但如果资源不存在(服务器已关闭),我的承诺似乎永远不会被解决/拒绝。代码如下所示:

//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
});
// ...

0 个答案:

没有答案