<html>
    <head>
      <base href="https://bugs.webkit.org/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Promise - MutationObserver interaction spins the event loop"
   href="https://bugs.webkit.org/show_bug.cgi?id=163845">163845</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Promise - MutationObserver interaction spins the event loop
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>WebKit
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>Safari Technology Preview
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Macintosh
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>OS X 10.11
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>Normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P2
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>JavaScriptCore
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>webkit-unassigned&#64;lists.webkit.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>miklos.bertalan&#64;risingstack.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Queueing a Promise from a MutationObserver handler or triggering an observed change from a Promise callback spins the event loop. According to the spec, it should register a new microtask and not a new task.

Code to reproduce (it is also attached):

&lt;div class=&quot;tester&quot;&gt;&lt;/div&gt;
&lt;script&gt;
const tester = document.querySelector('.tester')
let counter = 0

setTimeout(() =&gt; console.log('timeout'))

new MutationObserver(() =&gt; {
  console.log('mutation')
  if (counter &lt; 100) {
    Promise.resolve().then(() =&gt; {
      console.log('promise')
      tester.setAttribute('data-random', Math.random())
    })
  }
  counter++
}).observe(tester, {attributes: true})

tester.setAttribute('data-random', Math.random())
&lt;/script&gt;

Issue:

It the above code should print: mutation, promise, mutation, promise, ... timeout
Instead it prints: mutation, timeout, promise, mutation, promise, mutation, ...

Notes:

It seems to get it wrong consistently (not just some of the time). I think it is not caused by any kind of throttling.
It seems to queue a new task for both directions (Promise -&gt; Mutation, Mutation -&gt; Promise).
(Promise -&gt; Promise) and (Mutation -&gt; Mutation) are both handled correctly as new microtasks.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are the assignee for the bug.</li>
      </ul>
    </body>
</html>