<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Thới Hải Đức</title>
    <description>The latest articles on DEV Community by Thới Hải Đức (@haiduc0147).</description>
    <link>https://dev.to/haiduc0147</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4219774%2Fc46541f1-1cfb-4246-9cb3-26c1f0faf983.jpg</url>
      <title>DEV Community: Thới Hải Đức</title>
      <link>https://dev.to/haiduc0147</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/haiduc0147"/>
    <language>en</language>
    <item>
      <title>Why does setTimeout(0) run after your Promise? The event loop, step by step</title>
      <dc:creator>Thới Hải Đức</dc:creator>
      <pubDate>Sun, 01 Nov 2026 00:28:04 +0000</pubDate>
      <link>https://dev.to/haiduc0147/why-does-settimeout0-run-after-your-promise-the-event-loop-step-by-step-3m0p</link>
      <guid>https://dev.to/haiduc0147/why-does-settimeout0-run-after-your-promise-the-event-loop-step-by-step-3m0p</guid>
      <description>&lt;p&gt;Here's a small JavaScript puzzle. What order do these letters print in?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;A&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;B&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;C&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;D&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;E&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;F&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Take a guess before you scroll.&lt;/p&gt;

&lt;p&gt;.&lt;/p&gt;

&lt;p&gt;.&lt;/p&gt;

&lt;p&gt;.&lt;/p&gt;

&lt;p&gt;The answer is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A D F C E B
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;B&lt;/code&gt; has a delay of &lt;code&gt;0&lt;/code&gt;, but it prints last. To see why, you need to know four things.&lt;/p&gt;

&lt;h2&gt;
  
  
  The four pieces
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The call stack&lt;/strong&gt; holds the code running right now. JavaScript runs one piece of code at a time on the main thread, and it finishes the current job before starting another.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Host APIs&lt;/strong&gt; are provided by the browser or Node, not the JavaScript language. Timers are one example. They wait outside the call stack, so waiting doesn't block your code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The macrotask queue&lt;/strong&gt; (also called the task queue) holds jobs such as timer callbacks. The event loop takes one at a time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The microtask queue&lt;/strong&gt; holds jobs such as Promise callbacks and the code that continues after an &lt;code&gt;await&lt;/code&gt;. It's drained completely before the next macrotask runs.&lt;/p&gt;

&lt;p&gt;The event loop is the rule that connects them:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Run the current task until the call stack is empty.&lt;/li&gt;
&lt;li&gt;Run every microtask, including any new ones added along the way.&lt;/li&gt;
&lt;li&gt;Take one macrotask and run it.&lt;/li&gt;
&lt;li&gt;Repeat.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Walking through the code
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step 1: &lt;code&gt;console.log("A")&lt;/code&gt;.&lt;/strong&gt; This is synchronous, so it prints right away.&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;A&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: &lt;code&gt;setTimeout(..., 0)&lt;/code&gt;.&lt;/strong&gt; The timer is handed to the host. Its callback, which prints &lt;code&gt;B&lt;/code&gt;, can't run until the current code has finished. It goes into the macrotask queue when the timer expires.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: &lt;code&gt;Promise.resolve().then(...)&lt;/code&gt;.&lt;/strong&gt; The Promise is already fulfilled, so its callback, which prints &lt;code&gt;C&lt;/code&gt;, goes straight into the microtask queue. It doesn't run yet, because &lt;code&gt;.then&lt;/code&gt; callbacks never run synchronously.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4: &lt;code&gt;run()&lt;/code&gt;.&lt;/strong&gt; Calling an async function runs its body synchronously until the first &lt;code&gt;await&lt;/code&gt;. So &lt;code&gt;D&lt;/code&gt; prints immediately.&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;A D&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5: &lt;code&gt;await null&lt;/code&gt;.&lt;/strong&gt; This is the part that surprises people. &lt;code&gt;await&lt;/code&gt; pauses &lt;code&gt;run&lt;/code&gt; and queues the rest of the function, the line that prints &lt;code&gt;E&lt;/code&gt;, as a microtask. Control goes back to the code that called &lt;code&gt;run()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6: &lt;code&gt;console.log("F")&lt;/code&gt;.&lt;/strong&gt; This is still the original script, so it prints now.&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;A D F&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 7: the call stack is empty.&lt;/strong&gt; Now the microtask queue is drained, in order. &lt;code&gt;C&lt;/code&gt; was queued first, then &lt;code&gt;E&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;A D F C E&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 8: one macrotask runs.&lt;/strong&gt; The timer callback finally gets its turn.&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;A D F C E B&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What &lt;code&gt;setTimeout(fn, 0)&lt;/code&gt; really means
&lt;/h2&gt;

&lt;p&gt;It doesn't mean "run this now." It means "run this in a later task, as soon as possible." The delay is a minimum, not a guarantee.&lt;/p&gt;

&lt;p&gt;That matters in real code. If the call stack stays busy, or a long chain of microtasks keeps adding more microtasks, the timer has to wait. In a browser, the page can't repaint during that time either, which is how a page ends up frozen.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three things to remember
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Synchronous code always finishes first.&lt;/li&gt;
&lt;li&gt;All microtasks run before the next macrotask.&lt;/li&gt;
&lt;li&gt;Code before the first &lt;code&gt;await&lt;/code&gt; in an async function runs synchronously. Code after it doesn't.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  See it run
&lt;/h2&gt;

&lt;p&gt;Reading this is one thing, but it clicked for me when I could watch the queues change. I built a step-through version of this exact example, where you can watch the call stack, both queues and the console at every step:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://software-atlas.haiduc0147.dev/t/async-await#event-loop" rel="noopener noreferrer"&gt;Step through the event loop&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It's part of Software Atlas, a free site I'm building with interactive explanations of how software works. It has no ads and no sign-up.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
