Let’s see how the execution goes for this promise constructor.

new Promise((resolve) => {
	setTimeout(() => resolve("Done!"), 100);
})
.then(result => console.log(result));

We have the promise constructor that has a setTimeout and we also have a then handler.

let’s just go through it step by step and see what’s happens.

First, the new Promise constructor is added to the Call Stack

JS Context.png

1. 이벤트 루프 안의 큐들

이벤트 루프에는 크게 두 가지 계층이 있습니다:


2. 비교표

구분 Task Queue (MacroTask) MicroTask Queue NextTick Queue
속성 큰 단위 작업, 주기적 실행 작은 단위 후처리, 즉각 실행 Node.js 특수 큐, microtask보다 빠름
대표 예시 setTimeout, setInterval, setImmediate(Node), I/O Promise.then, queueMicrotask, MutationObserver process.nextTick
실행 타이밍 이벤트 루프의 각 tick 마다 1개 실행 → 끝나면 microtask 실행 현재 task가 끝나면 즉시 모두 실행 현재 task 끝 → microtask보다 먼저 실행
환경 브라우저 + Node.js 브라우저 + Node.js Node.js 전용
우선순위 낮음 (가장 마지막) 중간 (task 다음, task 시작 전 X) 최고 (task 직후, microtask 앞)

3. 실행 흐름 (순서 예시)

console.log("start");

setTimeout(() => console.log("timeout"), 0);

Promise.resolve().then(() => console.log("promise"));

queueMicrotask(() => console.log("microtask"));

process.nextTick(() => console.log("nextTick"));

console.log("end");

📌 Node.js에서의 출력 순서:

start
end
nextTick       // nextTickQueue → microTask보다 우선
promise        // microTaskQueue
microtask      // microTaskQueue
timeout        // Task Queue (MacroTask)

📌 브라우저에서의 출력 순서(NextTick 없음):