Why are Callbacks from Promise `.then` Methods an Anti-Pattern

You should change it to var getTokens = function() { return $http.get(‘/api/tokens’); }; And, then in other module use yourModule.getTokens() .then(function(response) { // handle it }); As to why it’s an anti-pattern, I’d say that, first, it doesn’t allow you to further chain your success/fail handler methods. Second, it handles the control of processing the … Read more

AngularJS : Initialize service with asynchronous data

Based on Martin Atkins’ solution, here is a complete, concise pure-Angular solution: (function() { var initInjector = angular.injector([‘ng’]); var $http = initInjector.get(‘$http’); $http.get(‘/config.json’).then( function (response) { angular.module(‘config’, []).constant(‘CONFIG’, response.data); angular.element(document).ready(function() { angular.bootstrap(document, [‘myApp’]); }); } ); })(); This solution uses a self-executing anonymous function to get the $http service, request the config, and inject it … Read more

Is this a “Deferred Antipattern”?

Is this a “Deferred Antipattern”? Yes, it is. ‘Deferred anti-pattern’ happens when a new redundant deferred object is created to be resolved from inside a promise chain. In your case you are using $q to return a promise for something that implicitly returns a promise. You already have a Promise object($http service itself returns a … Read more

How to access the value of a promise?

promiseA‘s then function returns a new promise (promiseB) that is immediately resolved after promiseA is resolved, its value is the value of the what is returned from the success function within promiseA. In this case promiseA is resolved with a value – result and then immediately resolves promiseB with the value of result + 1. … Read more