티스토리 뷰

반응형

다음과 같이 getter에 async 키워드를 작성할 경우에는 Syntax Error가 발생한다.

SyntaxError: Unexpected identifier

class MyClass {
    #props = { hello: 'world' };
    
    async get result() { // SyntaxError: Unexpected identifier
        return this.#props;
    }
}

(async function main() {
    const myInstance = new MyClass();
    
    const result = await myInstance.result;
    console.log(result);
})();

이 경우, 그냥 Promise를 반환하면 간단하게 async getter를 구현할 수 있다.

class MyClass {
    #props = { hello: 'world' };
    
    get result() {
        return Promise.resolve(this.#props);
    }
}

(async function main() {
    const myInstance = new MyClass();
    
    console.log(
        await myInstance.result
    ); // { hello: 'world' }
})();
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함