如何等待我的服务功能在 AngularJS 内完成?

How to wait for my services function to finish in AngularJS?

我做了一个有功能的服务。

app.service('getService', ['$http', function($http){    
    this.getProducts = function(log = true){
         $http({
             url: "/admin/products"
         }).then(function successCallback(response) {
             log == true ? console.log(response) : false;
             return response.data;
         }, function errorCallback(response) {
             log == true ? console.log(response) : false;
         });
    }
}]);

之后,我调用它并等待函数完成,但这不起作用。

app.controller('categoryCtrl', ['$scope','staticsService', function ($scope, getService) {
    turnonloader();
    $scope.model = getService.getProducts().then(function(){
        turnoffloader();
        console.log("Finished!");
    });
}]);

哪个是最好的解决方案?

服务应return承诺:

app.service('getService', ['$http', function($http){    
    this.getProducts = function(log = true){
         ̶$̶h̶t̶t̶p̶(̶{̶
         return $http({
             url: "/admin/products"
         }).then(function successCallback(response) {
             log == true ? console.log(response) : false;
             return response.data;
         }, function errorCallback(response) {
             log == true ? console.log(response) : false;
             //IMPORTANT
             throw response;
         });
    }
}]);

错误处理程序重新抛出错误响应也很重要。否则承诺将从拒绝转换为成功承诺 returns undefined.