Home >Web Front-end >JS Tutorial >Why are Callbacks Detrimental in AngularJS Promise Chains?
Why Use Callbacks in AngularJS Promise Chains is Detrimental
It is often suggested to provide a callback function to AngularJS services, as seen in the code snippet below:
However, this practice is discouraged as an Anti-Pattern. AngularJS services like $http return promises, and attaching callback methods to their .then methods constitutes an undesirable inversion of control.
Refactoring
To rectify this issue, modify the code as follows:
In the service module:
This refactoring eliminates the callback function and maintains the desired control flow through the .then method.
Justification for Refactoring
Callbacks as implemented in the original code:
The above is the detailed content of Why are Callbacks Detrimental in AngularJS Promise Chains?. For more information, please follow other related articles on the PHP Chinese website!