High-confusion async output
Promise vs setTimeout
See why Promise callbacks run before setTimeout callbacks, even when the timer delay is 0ms.
Synchronous code runs first, Promise callbacks run next as microtasks, and timer callbacks run after the microtask queue is empty.
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.
Promise before timeout
Microtasks run before timers after the stack clears.
setTimeout 0
Useful when you need to defer UI work until after the current event handler.
Timeout inside promise
This shows up in promise-based setup that schedules fallback timers.
Promise inside timeout
This affects promise work triggered inside DOM events, timers, and network callbacks.