Restangular Promise never resolved when server off

2019-07-27 03:06发布

问题:

I am calling a ressource with some parameters and everything works great. But if the ressource is not there (server is down) my Promise seems never to be resolved/rejected. Code looks like this:

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

How can when the Server is down neither the Service nor the Controller will access the catch() funciton. Any suggestions how to handle this?

======= UPDATE ==========

Thanks to Bergi, I got it working by looking at the deferred antipattern:

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