是否可以使getter方法异步?

时间:2020-05-20 20:02:29

标签: javascript class asynchronous server getter

我有一个类,用于存储该对象的特定值,例如名称。但是我不能在一开始就获得所有值,我只想在需要时才获得它们。因此,我添加了一个getter函数,该函数检查该值是否已被获取并返回。但是,如果没有(第一次),则需要从异步函数中检索它。无论如何有吸气剂做到这一点?就像Web服务器一样,它也不必阻止系统进程。

class a {

    get name () {
        if (this.storedName) return this.storedName;
        else asyncDBFunction((retreivedName)=>{
            return retreivedName;
        })
    }

    getValues () {
        return {name: this.name};
    }
};

var values = this.getValues();

有什么办法做到这一点,还是只需要将我的getter更改为带有回调的普通异步函数?

0 个答案:

没有答案
相关问题