Initially, when using basic $http, I had this code snippet in a service:
var fetchSomeData = 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;
}
Later on, I decided to switch to Restangular and modified the code as follows:
var fetchSomeData = function() {
var user = Restangular.one('mylongurl');
$timeout(function(){
return user.get().then(function (response) {
return response;
}, function(error){
return error;
});
}, 2000);
return user;
};
Then, in the controller, I used it like this:
someService.fetchSomeData().then()...
However, I encountered an issue with the timeout causing the following error:
someService.fetchSomeData().then is not a function