Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing .always().error() syntax for Angular $q promises
    text
    copied!<p>I'm implementing just a thin wrapper around $http for our REST API, and I'm wanting it to return a promise in the same way as $http does (after I massage the data).</p> <p>Here's my service:</p> <pre><code>Services.service('Item', ['$http', '$q', function($http, $q){ var deferred = $q.defer(); var getSuccess = function(data, status, headers, config){ var item = angular.copy(data); item.primaryImage = 'https://my.cdn.com/' + item.meta.images[0].s3id; if(item.meta.source_link !== null) { item.sourceLink = item.meta.source_link.url; } deferred.resolve(item, data, status, headers, config); }; var getError = function(data, status, headers, config) { deferred.reject(data, status, headers, config); }; this.get = function(userID, itemID) { $http({ method: 'GET', url: '/api/items/' + userID + '/' + itemID }).success(getSuccess).error(getError); return deferred.promise; }; }]); </code></pre> <p>But from my understanding of the <a href="http://docs.angularjs.org/api/ng.%24q" rel="nofollow">documentation</a>, I have to use <code>.then(success, error, always)</code> rather than <code>.success().error().always()</code> like I can with <code>$http</code>.</p> <p>Is it possible to implement promises in the same way that <code>$http</code> does? I would love to do this</p> <pre><code>var req = Item.get($routeParams.userID, $routeParams.itemID); req.success(function(item){ window.console.log('Got an item!', item); }); .error(function(item){ window.console.log('Damn. It failed.') }) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload