Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
Why does JS seem to empty the microtasks queue before the closing </script> tag?

While looking at this code:

<script>
    console.log(1);
    new Promise(r=>r()).then(() => console.log(2));
</script><script>
    console.log(3);
</script>

it seems like 2 should be printed after 3, because it should have been queued as a microtask, just like it behaves here:

    <script>
        console.log(1);
        new Promise(r=>r()).then(() => console.log(2));
        console.log(3);
    </script>

I can't find a good explanation - why would the JS engine bother to empty the microtask queue before continuing to process the next script element?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It's not the JS engine but the DOM one, which after running a classic script has to clean up after running a script (step 8). This clean up algorithm does a microtask checkpoint.

  1. If the JavaScript execution context stack is now empty, perform a microtask checkpoint.

So the microtask isn't really checked "before" the </script> end tag, but rather right after the script got executed (which technically happens when the end tag is met).

You can even make some funky stuff like forcing a MutationObserver to notify of changes in the DOM right in the middle of parsing an element's content This can come handy for testing.:

<script>
  function myObserver(mutationsList) {
    for (let mutation of mutationsList) {
      for (let n of mutation.addedNodes) {
        if (n.id === 'parent') {
          console.log("innerHTML: ", n.innerHTML);
        }
      }
    }
  }
  var observer = new MutationObserver(myObserver);
  observer.observe(document.body, {
    childList: true,
    subtree: true
  });
</script>
<div id="parent">
  <span>This will be parsed</span>
  <script>/* This script forces the MutationObserver to kick its notifications */</script>
  <span>Not yet parsed</span>
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!