π₯ Code Dojo
μ€μ νλ‘λμ κΈ μ½λλ₯Ό μ½κ³ , κ·Έ λ°μ μ¬κ³ λͺ¨λΈ(μ¬μ)μ λ³΄κ³ , νλΈλ¦Ώμμ βΆ λλ¬ μ§μ λλ €λ³Έλ€. νΈλ β λ 벨(κΈ°λ³Έ λ¬Έλ²βνλ‘λμ ) β λ μ¨. μ λΆ μμ΄.
The event loop β why a promise beats setTimeout(0)
JavaScript is single-threaded: async callbacks wait in queues. Microtasks (promises) are fully drained before any macrotask (setTimeout). So a promise scheduled LATER still runs before a 0ms timer scheduled earlier. Run it and read the order.
There are two queues with different priority. After the synchronous code finishes, the engine drains ALL microtasks (promise callbacks), then takes ONE macrotask (a timer/event). Output order is 1, 2, 3, 4 β the promise (3) jumps ahead of the timer (4).
μ€λ³λ‘ μμ΄λ‘ λ§νκΈ° π£
Promise.resolve().then(() => out.push("3 microtask"))setTimeout(() => out.push("4 macrotask"), 0)λ λ§μ μ€ν(Next.jsΒ·JavaΒ·SQLΒ·GoΒ·Python)κ³Ό λ°±μλ νλ¦λλ κ³μ μΆκ°λ©λλ€.