另一个完成执行后调用类方法

时间:2019-05-09 08:14:23

标签: javascript class methods callback

我试图在另一个方法完成后执行一个类方法。第一个调用API,并填充数组和字典。然后,下一个方法将获取数据并创建一个新的字典。我无法按顺序执行它,知道吗?

我尝试在调用第一个方法时添加回调,但是它不起作用。

this.fillListDictionary(function(){
    this.fillCatDictionary();
});

fillListDictionary(callback) {
    this._categoryList = [];
    this._propertyDictionary = [];

    //Fill list and dictionary calling the API ()
    callback();
}

fillCatDictionary() {
    this._catDictionary = [];        
    this._categoryList.forEach((category) => {
        this._propertyDictionary.forEach((property) => {        
            if(property.properties.includes(category)){
                var f_index = this._catDictionary.findIndex(x => x.category == category);
                if(f_index >= 0){
                    this._catDictionary[f_index].dbIds.push(property.dbId);
                }
                else {
                    var cat = new Object();
                    cat.category = category;
                    cat.dbIds = [property.dbId];
                    this._catDictionary.push(cat);
                }
            }
        })
    })
}

我想使其依次工作:fillCatDictionary必须在fillListDictionary完成工作后执行。

0 个答案:

没有答案
相关问题