0

In basic $http i had such code (this is a service):

var getSomeData = function() {
    var deferred = $q.defer();
    $timeout(function() {
      $http.get('...mylongurl', {
        headers: {
          'Content-Type': 'application/json'
        }
      })
      .success(function(response) {
        deferred.resolve(response);
      })
      .error(function(error) {
        deferred.reject(error);
      });
    }, 2000);
    return deferred.promise;
}

and i transformed it into restangular so:

var getSomeData = function() {
  var user = Restangular.one('mylongurl');
  $timeout(function(){
    return user.get().then(function (response) {
      return response;
    }, function(error){
      return error;
    });
  }, 2000);
  return user;
};

and then in controller i use it so:

someService.getSomeData().then()...

but now with timeout i get: someService.getSomeData().then is not a function

4
  • you should use the same deferred it dosent have automatically .then() Commented May 5, 2016 at 8:10
  • @Erez but restangular use promisses by default... Commented May 5, 2016 at 8:11
  • yes when you do user get but not the get some data function Commented May 5, 2016 at 8:11
  • @Erez example plz) Commented May 5, 2016 at 8:12

1 Answer 1

1

You can take advantage of the fact that $timeout returns a promise:

function getSomeData() {
  var user = Restangular.one('mylongurl');
  return $timeout(2000)
      .then(function(){
           return user.get();
       });
}

Older versions of angular may still require an empty function passed to the timeout $timeout(function(){}, 2000), but if you are using a recent one you can just omit it altogether.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.