如何确保函数仅在对象构建后才被调用?

How to make sure that a function is called only after an object is build?

提问人:I Love Stackoverflow 提问时间:3/20/2021 更新时间:3/23/2021 访问量:50

问:

我有一个具有通用功能的功能,但我正在使用和创建的功能如下:common service.js

this.createFastlookupDictionaryForEmployee = function (data) {
        --code to build dictionary
        return dictionaryArr;
    }
    
    this.getEmployeeId = function (dictionaryArr,employeeId) {
        return dictionaryArr[employeeId].
    }

控制器

function onPageLoad(Callback1,
                Callback2,
                Callback3) {
                myService.getEmloyeeList().success(function (data) {
                    $scope.employeeLookupList = commonService.createFastlookupDictionaryForEmployee(data.items);
                    Callback1();
                    Callback2();
                }).finally(function () {
                });
            }
            
  $scope.getEmployeeId = function () { //callback function 1
                myService.getEmployeeData().success(function (data) {
                    var employeeId = $scope.employeeLookupList[10]; //error here as the $scope.employeeLookupList is not populated yet
                }).finally(function () {
                });
            };

所有回调函数都从数组对象访问数据。现在的问题是,由于 API 调用返回大量数据,这就是为什么构建 Dictionary 需要时间的原因,回调函数调用已完成,因此无法读取某些员工 ID。$scope.employeeLookupListgetEmloyeeList

我无法将回调传递给方法,因为它在很多地方都在使用。createFastlookupDictionaryForEmployee

我如何确保字典被创建,并且只有在那之后我的遗嘱才会被调用?Callback1, Callback2

我不想在填充字典之前调用 Callback1 和 Callback2。

JavaScript AngularJS 回调

评论


答:

0赞 JPeter 3/20/2021 #1

您应该像第一次服务一样打电话给您的。话虽如此,您应该使用 / 等待它的响应:Callback'smyService.getEmloyeeList()successfinally

this.createFastlookupDictionaryForEmployee = function (data) {
        var defer = $q.defer();
        // code to build dictionary

        // after dictionary been resolved
        defer.resolve(dictionaryArr);

        // if you need to throw an error you can use it like
        // defer.reject("error");

        return defer.promise;
    }

myService.getEmloyeeList().success(function (data) {
 $scope.employeeLookupList = [];
 commonService.createFastlookupDictionaryForEmployee(data.items).then(function (data) {
           $scope.employeeLookupList = data.items; //response from service
           // Do something when request comes with success response
           // I think you should then receive the data on your callback's
           Callback1(data.items);
           Callback2(data.items);
      })['catch'](function (resp) {
         //Do something after error responses
      })['finally'](function () {
         //Do something after success or error responses
      });
}).finally(function () {
});

这样,函数将仅在使用请求中的数据成功时调用。在此之后,您可以观察以检查它是否为空。 快乐编码:)Callback's$scope.employeeLookupList

评论

0赞 I Love Stackoverflow 3/20/2021
createFastlookupDictionaryForEmployee 不在 API 调用中,那么为什么我们将其包装在“.success”中?
0赞 I Love Stackoverflow 3/20/2021
我在“commonService.createFastlookupDictionaryForEmployee(data.items).success”上收到错误,说“它不是一个函数”
0赞 JPeter 3/22/2021
就像在您的示例中一样,您确实在成功中使用了它,您应该在您的服务 commonService rigth 中有一个服务 createFastlookupDictionaryForEmployee(data.items)?您的请求需要有一个 http 调度程序,以便您可以使用 .sucess 或最终
0赞 I Love Stackoverflow 3/23/2021
但是 createFastlookupDictionaryForEmployee(data.items) 是一个普通的 javascript 函数,因此它没有 HTTP 调度程序。createFastlookupDictionaryForEmployee(data.items)方法与我在问题中发布的方法完全相同
0赞 JPeter 3/23/2021
哦,我现在没有勇气。检查我的编辑。 ['catch] 和 ['finally'] 可能会因 angularjs 版本而有所不同