為什么承諾的回調是反模式的?我在StackOverflow上看到了答案,人們建議為AngularJS服務提供回調函數。app.controller('tokenCtrl', function($scope, tokenService) {
tokenService.getTokens(function callbackFn(tokens) {
$scope.tokens = tokens;
});});app.factory('tokenService', function($http) {
var getTokens = function(callbackFn) {
$http.get('/api/tokens').then (function onFulfilled(response) {
callbackFn(response.data);
});
};
return {
getTokens: getTokens };});在我看來,這是一種反模式。這個$http服務返回承諾.then方法執行回調函數感覺就像不健康的控制反轉。如何再因素這樣的代碼,以及如何解釋為什么最初的方法是不是個好主意?
為什么承諾的回調是反模式的?
滄海一幻覺
2019-07-11 12:40:23