defer.promise 在 angularJS 中返回相同的结果

defer.promise returning same result in angularJS

我有一个从 API.When 获取数据的服务 我正在尝试调用此服务。它返回相同的值。

appName.service('FetchCustomerDate', ['$http', '$q', function($http, $q) {
    var self = this;
    self.getCustomerData = function(token,name) {
        var deferred = $q.defer();
        return $http({
            method: 'GET',
            url: ,
            headers: {
                "Authorization": token,
                "x-xcmc-auth": ''
            }
        }).then(function(response) {
            deferred.resolve(response);
            return deferred.promise;
        }, function(response) {
            deferred.reject(response);
            return deferred.promise;
        });
    };
}]);

我在这里看到了一些混乱。让我们尝试清除它。如果你想使用延迟对象,你需要稍微改变你的代码:

appName.service('FetchCustomerDate', ['$http', '$q', function ($http, $q) {
    var self = this;
    self.getCustomerData = function (token, name) {
        var deferred = $q.defer();
        $http({ // Do not return  here, you need to return the deferred.promise
            method: 'GET',
            url: '...some URL here...',
            headers: {
                "Authorization": token,
                "x-xcmc-auth": ''
            }
        }).then(function (response) {
            deferred.resolve(response); // It's correct, you are resolving the deferred promise here.
            // return deferred.promise; // You do not need to return the deferred.promise here.
        }, function (response) {
            deferred.reject(response); // It's correct, you are rejecting the deferred promise here.
            // return deferred.promise; // You do not need to return the deferred.promise here.
        });

        return deferred.promise; // The function must return the deferred.promise
    };
}]);

具体来说,函数 getCustomerData 必须 return 属于 deferred 对象的承诺 return deferred.promise。在 then() 回调中,您只需解决或拒绝 deferred 承诺。您不需要 return deferred.promise.

您可以改进代码。 $http 服务 return 是一个承诺,由 then 回调编辑的值 return 由承诺中的 then 方法包装。知道这一点,您可以删除 deferred object:

的使用
appName.service('FetchCustomerDate', ['$http', function ($http) {
    var self = this;
    self.getCustomerData = function (token, name) {
        return $http({ // Here, you need to return the promise returned by $http. Than promise will contain the response returned inside "then" callbacks.
            method: 'GET',
            url: '...some URL here...',
            headers: {
                "Authorization": token,
                "x-xcmc-auth": ''
            }
        }).then(function (response) {
            return response; // Simply return the response, it will be wrapped in a resolved promise by "then()"
        }, function (response) {
            return response; // Simply return the response, it will be wrapped in a rejected promise by "then()"
        });
    };
}]);

如您所见,2 then 回调只是 return 对象 response,因此您可以省略它们:

appName.service('FetchCustomerDate', ['$http', function ($http) {
    var self = this;
    self.getCustomerData = function (token, name) {
        return $http({ // Here, you need to return the promise returned by $http. Than promise will contain the response form the GET call
            method: 'GET',
            url: '...some URL here...',
            headers: {
                "Authorization": token,
                "x-xcmc-auth": ''
            }
        });
    };
}]);

通常当您使用 $http 服务获取数据时,您希望从响应中获取数据并将其影响到 $scope 等,或者以某种方式处理它。你想做什么?请澄清您的问题。

通常情况下,提取看起来像这样:

appName.service('FetchCustomerDate', ['$http', '$q', function($http, $q) {
    var self = this;

    function notifyError(reason) {
      console.error(reason);
    }

    self.getCustomerData = function(token,name) {
        var deferred = $q.defer();
        return $http({
            method: 'GET',
            url: ,
            headers: {
                "Authorization": token,
                "x-xcmc-auth": ''
            }
        })
          .then(function onSuccess(response) {
            var cfg = response.data; // process data 
        })
          .then(function onSuccess(response) {
            // chained promises
        })
          .then(
            function onSuccess(res) {
              // ... this will trigger the chain reaction
              deferred.resolve(res);
            },
            function onFailure(reason) {
              notifyError(reason); // manage the error
              deferred.reject(reason);
            })
          ;
          return deferred.promise;
        }
    }]);