Return deferred.promise 返回 2 个服务

Return deferred.promise 2 services back

我有 3 个服务。

  1. HttpSender - 它控制 $http 请求

    app.service("HttpSender", ["$http", "$q", function ($http, $q) {
    
        this.send = function (path, method, params) {
            var deferred = $q.defer();
            $http({
                url: path,
                method: method,
                params: params
            }).then(function successCallback(response) {
                deferred.resolve(response.data);
            }, function errorCallback(response) {
                deferred.reject(response);
            });
            return deferred.promise;
        };
    
        this.sendRequestWithFile = function (path, method, params) {
            //todo check if needed
        };
    
    }]);
    
  2. Api - 控制所有 api/ 访问令牌进程

      service("API", ["HttpSender", "$q", 'WindowOpen', function(HttpSender, $q, WindowOpen){
    
    var self = this;
     var API = {};
    
        API.requestTypes = {
            GetMethod: "GET",
            PostMethod: "POST",
            DeleteMethod: "DELETE",
            PutMethod: "PUT"
        };
    
            API.sendRequest = function (path, method, parameters, isCheckAccessToken)
            {
                path = ServersConfig.getApiServerUrl() + path;
                parameters.access_token = getAccessToken();
                HttpSender.send(path, method, parameters);
            };
    
    
            return API;
    
        }]);
    
  3. Api - 激活 api 请求的端点

      app.factory('SelectedEndpoint', ['API', 
    
                   function (API) {
    
                    var getPath = function (campaign) {
                        return "/campaigns/" + campaign.id + '/content/selected';
                    };
    
                    return {
                        get: function (campaign) {
                            API.sendRequest(getPath(campaign),  API.requestTypes.GetMethod, {}, true).then(function (content) {
    
    
                            });
                        }
                    };
    
    
              }]);
    

如何 return deferred.promise 到端点函数,然后才能得到答案?以下过程仅在我添加 then also in the api factory then return it to the endpoint

时才有效

How can return the deferred.promise to the endpoint function so the then will get the answer?

通过正确地return在每一步做出承诺。

1. HttpSender 服务。这里不用deferred,直接return promise:

app.service("HttpSender", ["$http", "$q", function($http, $q) {

  this.send = function(path, method, params) {
    return $http({
      url: path,
      method: method,
      params: params
    }).then(function successCallback(response) {
      return response.data;
    });
  };

}]);

2. Api 服务。确保你 return 之前承诺 return HttpSender.send(path, method, parameters);:

 service("API", ["HttpSender", "$q", 'WindowOpen', function(HttpSender, $q, WindowOpen) {

  var self = this;
  var API = {};

  API.requestTypes = {
    GetMethod: "GET",
    PostMethod: "POST",
    DeleteMethod: "DELETE",
    PutMethod: "PUT"
  };

  API.sendRequest = function(path, method, parameters, isCheckAccessToken) {
    path = ServersConfig.getApiServerUrl() + path;
    parameters.access_token = getAccessToken();
    return HttpSender.send(path, method, parameters); // note return promise
  };

  return API;

}]);