출처: 이웅모 《모던 자바스크립트 Deep Dive》 45장 · 본인 말로 정리

한줄 요약

Promise는 비동기 결과(성공/실패)를 객체로 감싸 then/catch 체이닝으로 콜백 지옥·에러·병렬 조합을 다루기 쉽게 한다.

콜백의 한계

중첩(콜백 헬), 에러 처리 분산, 여러 비동기 조합 어려움. 비동기 함수는 즉시 return되므로 콜백 안 값을 동기적으로 못 꺼냄.

생성과 상태

const p = new Promise((resolve, reject) => {
  if (ok) resolve(value);
  else reject(error);
});

pending → fulfilled 또는 rejected. settle 후 불변.

후속 처리

promise
  .then(v => next(v))
  .catch(err => handle(err))
  .finally(() => cleanup());

then은 새 프로미스 반환 → 체이닝. 값/프로미스 반환·throw 규칙 기억.

정적 메서드

마이크로태스크

프로미스 콜백은 타이머(매크로)보다 우선.

setTimeout(() => console.log('macro'), 0);
Promise.resolve().then(() => console.log('micro'));
console.log('sync');
// sync → micro → macro

fetch