Skip to main content

High-confusion async output

Promise vs setTimeout

See why Promise callbacks run before setTimeout callbacks, even when the timer delay is 0ms.

Short answer

Synchronous code runs first, Promise callbacks run next as microtasks, and timer callbacks run after the microtask queue is empty.

People search this as
Promise vs setTimeoutsetTimeout 0 promise orderwhy promise runs before timer

Why developers get surprised

A 0ms timer does not mean immediate execution. It only schedules a callback for a future timer phase after the current stack and microtasks finish.

Where this appears in real apps

This shows up in React state updates, toast cleanup, test assertions, retry timers, analytics flushing, and UI code that mixes Promise work with timer-based fallback logic.

Fix direction

When order matters, use one scheduling model or make the dependency explicit with await, returned promises, or a dedicated helper.

Practice this topic

Start with the strongest case, then continue through related examples.

Search all cases