Can ES6's Class stored value function (setter) be asynchronous?

get prop() {
    return this.list
}

set prop(pm = {}) {
    ReqApi.get({
        url: Urls.GET_ENQUIRYLIST,
        pm
    })
        .then(res => {
            this.list = res.list
        })
}

this doesn"t seem to work!

Mar.16,2021

setter can be asynchronous internally, but since it is asynchronous, I'm afraid it can't be obtained immediately after setting the value. If you use setTimeout to simulate asynchronous calls,

const target = {
    // ....

    async setTest(data) {
        return new Promise(resolve => {
            setTimeout(() => {
                this.data = data;
                resolve();
            }, 1000);
        });
    }
};

(async () => {
    await target.setTest("hi");
    console.log(console.log(target.test));
})();

the output here has been waiting until the end of the asynchronous process

Menu