Home > Article > Web Front-end > Why are Callbacks in Promise .then Methods an Anti-Pattern in AngularJS?
Question:
Is it an anti-pattern to provide callback functions to AngularJS services within promise .then methods? If so, how should the code be refactored and why?
Answer:
Re-factor the Code:
Change the getTokens method in the tokenService to return the promise directly instead of accepting a callback:
var getTokens = function() { return $http.get('/api/tokens'); };
In the controller, use the .then method to chain success/fail handlers:
yourModule.getTokens() .then(function(response) { // handle it });
Why the Original Way was an Anti-Pattern:
The above is the detailed content of Why are Callbacks in Promise .then Methods an Anti-Pattern in AngularJS?. For more information, please follow other related articles on the PHP Chinese website!