Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 모던 자바스크립트
- Deep Dive
- 딥다이브
- TypeORM
- this
- bucket4j
- 우아한테크코스
- 유효시간 설정 url
- NestJS
- 프론트엔드 과제
- concurrency limit
- 음악 url 파일 다운로드
- api 요청 수 제한
- 우아한 테크코스
- 스프링부트
- 타입스크립트
- 프론트엔드
- 자바스크립트
- Dev-Matching
- invalid_grant
- AWS
- compateto
- 파일 url
- 프로그래머스
- 검색
- oauth
- redis
- 프리코스
- api 비동기처리
- 코멘토 #코멘토실무PT #실무PT후기 #실무강의 #리액트강의 #웹프로그래밍 #react #웹개발실무
Archives
- Today
- Total
개발 알다가도 모르겠네요
상태가 결정된 Promise 객체 본문
728x90
fulfilled 상태의 Promise 객체 만들기
const p = Promise.resolve('success');
rejected 상태의 Promise 객체 만들기
const p = Promise.reject(new Error('fail'));
이미 fulfilled 또는 rejected 상태가 결정된 Promise 객체라도 then 메소드를 붙이면, 콜백에서 해당 작업 성공 결과 또는 작업 실패 정보를 받아올 수 있습니다. 시점과는 전혀 상관이 없습니다.
const p = new Promise((resolve, reject) => {
setTimeout(() => { resolve('success'); }, 2000); // 2초 후에 fulfilled 상태가 됨
});
p.then((result) => { console.log(result); }); // Promise 객체가 pending 상태일 때 콜백 등록
setTimeout(() => { p.then((result) => { console.log(result); }); }, 5000); // Promise 객체가 fulfilled 상태가 되고 나서 콜백 등록
Promise 객체의 상태가 fulfilled 또는 rejected 상태이기만 하면, 어느 시점이든, 몇 번이든 then 메소드를 붙여서 해당 결과를 가져올 수 있습니다.