Promise

Nammm·2022년 2월 21일
0

Promise란?

프로미스가 생성된 시점에는 알려지지 않았을 값을 위한 대리자이다. 비동기 연산이 종료된 이후에 결과 값과 실패 사유를 처리하기 위한 처리기를 연결할 수 있다. 프로미스를 사용하면 비동기 메서드에서 동기메서드처럼 값을 반환할 수 있다. 다만, 최종 결과를 반환하는것이 아닌, 미래의 어떤 시점에 결과를 제공하겠다는 '약속(프로미스)'를 반환한다.

Promise의 상태

Promise는 세가지 상태가 있으며, 이중 하나를 가짐.

  • 대기(Pending): 이행하지도, 거부하지도 않은 초기 상태.
  • 이행(Fulfilled): 연산이 성공적으로 완료됨.
  • 거부(rejected): 연산이 실패함.

Promise 상태의 내부작동

States

Promises have three possible mutually exclusive states: fulfilled, rejected, and pending.

  • A promise is fulfilled if promise.then(f) will call f "as soon as possible."
  • A promise is rejected if promise.then(undefined, r) will call r "as soon as possible."
  • A promise is pending if it is neither fulfilled nor rejected.

We say that a promise is settled if it is not pending, i.e. if it is either fulfilled or rejected. Being settled is not a state, just a linguistic convenience.

  • promise = fulfilled는 fulfill이 ASAP 실행된다.
  • promise = rejected는 r(rejected)가 ASAP 실행된다
  • promise = pending은 fulfilled

즉,

  • Pending 상태의 Promise는 값과 함께 fullfiled(이행)할 수도, error로 rejected(거부)할수도 있다.

  • fullfiled(이행)이나 rejected(거부)될 때, 프로미스의 then 메서드에 의해 대기열(큐)에 추가된 처리기들이 호출됩니다.

0개의 댓글