Using promises with Restangular
Restangular, the extremely popular REST API extension to AngularJS, takes a much more promise-centric approach compared to $resource
.
How to do it…
The Restangular REST API mapping will always return a promise. This is shown here:
(app.js) angular.module('myApp', ['restangular']) .controller('Ctrl', function($scope, Restangular) { Restangular .one('widget', 4) // get() will return a promise for the GET request .get() .then( function(data) { // consume response data in success handler $scope.status = 'One widget success!'; }, function(response) { // consume response message in error handler $scope.status = 'One widget failure!'; } ); // generally, the API mapping is stored in a variable, // and the promise-returning method will be invoked as needed var widgets = Restangular.all('widgets'); // create the request promise widgets.getList() .then(function(widgets) { // success handler ...