Promise 복습

이정민·2022년 5월 12일
0

promise 객체는 비동기 작업이 맞이할 미래의 완료 또는 실패와 그 결과값을 나타낸다.
함수에 콜백을 전달하는 대신 콜백을 첨부하는 방식의 객체

chaining 할 수 있다. 하나나 두 개 이상의 비동기 작업을 순차적으로 실행해야하는 상황에서 promise chain을 이용하여 해결
then()함수는 새로운 promise를 반환한다.

const promise2 = dosomething().then(successCallback, failureCallback)
doSomething()
.then(result => doSomethingElse(result))
.then(newResult => doThirdThing(newResult))
.then(finalResult => {
  console.log(`Got the final result: ${finalResult}`);
})
.catch(failureCallback);

promise의 세가지 상태

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

0개의 댓글