Search code examples
javascriptangularjsangular-promiserestangulardeferred

Using Restangular with promises (deferred)


In my app i'm using restangular, and i have such method (which i converted from the plain $http request). And i don't know how to use correctly restangular with promises. How is it possible?

Here is my code:

var test = function(){
var data = '{"Office":"' + office + '"}';
var deferred = $q.defer();
var person = Restangular.one('persons', id)
$scope.person.patch(data).then(function (response) {
  deferred.resolve(response);
},function (err, status) {
  deferred.reject(status);
});
return deferred.promise;
}


var runIt = function(){
    test.then(...)...
}

Solution

  • You could simply use promise returned by patch method of Restangular.one instead of creating a new custom promise.

    Code

    var test = function() {
        var data = '{"Office":"' + office + '"}';
        var person = Restangular.one('persons', id);
        //returned promise
        return person.patch(data).then(function(response) {
            return response.data; 
        }, function(err, status) {
            return response.status;
        });
    }
    
    test().then(...)