等待异步调用在其他地方启动时返回

时间:2014-12-12 20:27:12

标签: javascript angularjs asynchronous

我试图找到答案并开始阅读承诺/延期,但是当从其他地方开始时,我不知道如何处理这个问题。

    angular.module('myapp.utilities').factory('codetabelService', ['Restangular', function(Restangular) {
    var initialized = false;
    var listtopopulate1 = [];
    var listtopopulate2 = [];

    var update = function() {
        Restangular.all('codeTabel').getList()
            .then(function(codetabellen) {
                codetabellen.forEach(function(entry) {
                    //do some processing on return values 
                    listtopopulate1.push(entry);
                    listtopopulate2.push(entry);
                });
                initialized=true;
            });
    };

    return {
        initialize: function() {
            if (!initialized) {
                update();
            }
        },
        getListValuesType1: function() {
            //How do I wait here for initialized to become true?
            return listtopopulate1;
        },
        getListValuesType2: function() {
            //How do I wait here for initialized to become true?
            return listtopopulate2;
        }
    };
}]);

因此,我尝试做的是在单页应用启动时缓存一些值。 在我的根控制器上,我调用initialize方法,该方法启动对后端的异步调用。 当我的视图被加载并且控制器将范围值设置为getListValuesType1()的结果时,异步调用有时尚未完成。

因为调用方法getListValuesType1()的控制器没有触发异步加载,所以我不确定promises是否适用于此(我承认,我还是新手)

我发现了you can put a timer on it,但这似乎并不合适。只是觉得那里有更好的解决方案。

2 个答案:

答案 0 :(得分:1)

是的,您可以有效地使用承诺和承诺缓存来实现此目的,通过以下方式实现此目的: -

angular.module('myapp.utilities').factory('codetabelService', ['Restangular', '$q', function(Restangular, $q) {

    var initialized;//Use this to cache the promise
    var listtopopulate1 = [];
    var listtopopulate2 = [];

    var _initialize = function() {
       //If already initialized just return it which is nothing but the promise. This will make sure initialization call is not made
       return initialized || (initialized= Restangular.all('codeTabel').getList()
            .then(function(codetabellen) {
                codetabellen.forEach(function(entry) {
                   listtopopulate1.push(entry);
                   listtopopulate2.push(entry);
                });
              //Note- You could even return the data here
            }, function(){
                //Just clean up incase call is a failure.
                initialized = null;
                //Just reject with something if you want to:-
                //return $q.reject("SomeError")
            }));
    };

    return {
        initialize: function() {
            return _initialize(); //Just return promise incase you want to do somthing after initialization
        },
        getListValuesType1: function() {
         return _initialize().then(function(){  //return promise with a chain that resolves to the list
            return listtopopulate1;
         });
        },
        getListValuesType2: function() {
         return _initialize().then(function(){ //return promise with a chain that resolves to the list
            return listtopopulate2;
         });
        }
    };
}]);

使用它时,您可以: -

codetabelService.getListValuesType1().then(function(list){
      $scope.list1 = list;
});

有了这个,您甚至可以从合同中删除initialize调用,并仅在首次使用getListX方法时进行ajax调用。

答案 1 :(得分:0)

承诺将为此工作。你可能需要重构一些东西。

angular.module('myapp.utilities').factory('codetabelService', ['Restangular', function(Restangular) {
    var initialized = false;
    var listtopopulate1 = [];
    var listtopopulate2 = [];

    var update = function() {
        return Restangular.all('codeTabel').getList()
            .then(function(codetabellen) {
                codetabellen.forEach(function(entry) {
                    //do some processing on return values 
                    listtopopulate1.push(entry);
                    listtopopulate2.push(entry);
                });
                initialized=true;
            });
    };

    return {
        initialize: function() {
            if (!initialized) {
                this.updatePromise = update();
            }
        },
        getListValuesType1: function() {
            //How do I wait here for initialized to become true?
            return this.updatePromise.then(function() {
              // you'll want to refactor the callee to handle a promise here
              return listtopopulate1;              
            });
        },
        getListValuesType2: function() {
            return this.updatePromise.then(function(){
              // you'll want to refactor the callee to handle a promise here
              //How do I wait here for initialized to become true?
              return listtopopulate2;
            });
            //How do I wait here for initialized to become true?

        }
    };
}]);