티스토리 뷰
반응형
다음과 같이 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' }
})();
'Dev. > javascript' 카테고리의 다른 글
[JS] Promise가 아닌 값에 대한 await는 어떻게 처리될까? (0) | 2022.12.15 |
---|---|
[JS] function call, apply, bind 차이 (0) | 2022.12.02 |
[JS] 값이 null 또는 undefined인지 확인하기 (0) | 2022.10.04 |
[JS] 객체 배열을 문자열 기준으로 정렬하기 (0) | 2022.09.19 |
[JS] 중첩 클래스 정의하기 (0) | 2022.09.07 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- mysql
- hashicorp
- spring boot
- shell
- JEST
- terraform
- Puppeteer
- Docker
- etc
- postgresql
- Java
- Node.js
- Database
- RancherDesktop
- eureka
- jQuery
- AWS IoT
- pgloader
- react
- 코딩테스트
- javascript
- kotlin
- JPA
- Linux
- Git
- AWS
- Spring Cloud Config
- IntelliJ
- Gradle
- Vault
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함