<story><title>JavaScript Promises in Wicked Detail</title><url>http://mattgreer.org/articles/promises-in-wicked-detail/</url></story><parent_chain><item><author>exogen</author><text>The situation caused by promises gaining popularity and non-IE browsers refusing to implement setImmediate is really quite ugly. setTimeout&amp;#x27;s minimum value is defined to be 4ms by the HTML5 spec, so if the browser conforms to that and your app makes heavy use of promises (using setTimeout), now you&amp;#x27;re waiting multiple screen refreshes for updates – so you don&amp;#x27;t actually want to use setTimeout. Even though the asynchronous requirement is good for developers using the promise API, it really screwed over implementors.&lt;p&gt;The setImmediate polyfills attempt a bunch of ugly fallbacks, including (depending on which one you use): yield&amp;#x2F;generators, MutationObserver, postMessage&amp;#x2F;MessageChannel, onreadystatechange, and finally setTimeout (the slowest).&lt;p&gt;IMO the browser vendors should just give in and add setImmediate – if ever there was a need for it, this is it. But maybe they consider ES6 features a good enough substitute, I don&amp;#x27;t know.</text></item></parent_chain><comment><author>tav</author><text>Whilst the concept of setImmediate is nice, the current implementation in IE isn&amp;#x27;t properly integrated with the rest of the event loop — resulting in broken behaviour when you use it in combination with setTimeout [1], DOM events, etc.&lt;p&gt;In contrast, using MutationObserver results in correct behaviour on all modern browsers and relatively minimal delays — between 0.002ms and 0.007ms according to an OS X only micro-benchmark I did last month [2].&lt;p&gt;And, yes, it would be great if we could call a builtin instead of hacking on top of MutationObserver, but it isn&amp;#x27;t &lt;i&gt;that&lt;/i&gt; ugly:&lt;p&gt;&lt;pre&gt;&lt;code&gt;  if MutationObserver
    $div = root.document.createElement &amp;#x27;div&amp;#x27;
    observer = new MutationObserver tick
    observer.observe $div, attributes: true
    scheduleTick = -&amp;gt;
      $div.setAttribute &amp;#x27;class&amp;#x27;, &amp;#x27;tick&amp;#x27;
      return
  else
    scheduleTick = -&amp;gt;
      setTimeout tick, 0
      return
&lt;/code&gt;&lt;/pre&gt;
In conclusion, I agree that a feature like setImmediate would be great. But given IE&amp;#x27;s broken implementation and a viable workaround in modern browsers, I see no need to rush it. I&amp;#x27;d rather they focused on: new features like Object.observe; improving the performance of old features like Object.seal; and finalising some of the ES7 ideas like exposing the event loop!&lt;p&gt;[1] &lt;a href=&quot;http://codeforhire.com/2013/09/21/setimmediate-and-messagechannel-broken-on-internet-explorer-10/&quot; rel=&quot;nofollow&quot;&gt;http:&amp;#x2F;&amp;#x2F;codeforhire.com&amp;#x2F;2013&amp;#x2F;09&amp;#x2F;21&amp;#x2F;setimmediate-and-messagech...&lt;/a&gt;&lt;p&gt;[2] &lt;a href=&quot;https://gist.github.com/tav/9719011&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;gist.github.com&amp;#x2F;tav&amp;#x2F;9719011&lt;/a&gt;</text></comment>
<story><title>JavaScript Promises in Wicked Detail</title><url>http://mattgreer.org/articles/promises-in-wicked-detail/</url></story><parent_chain><item><author>exogen</author><text>The situation caused by promises gaining popularity and non-IE browsers refusing to implement setImmediate is really quite ugly. setTimeout&amp;#x27;s minimum value is defined to be 4ms by the HTML5 spec, so if the browser conforms to that and your app makes heavy use of promises (using setTimeout), now you&amp;#x27;re waiting multiple screen refreshes for updates – so you don&amp;#x27;t actually want to use setTimeout. Even though the asynchronous requirement is good for developers using the promise API, it really screwed over implementors.&lt;p&gt;The setImmediate polyfills attempt a bunch of ugly fallbacks, including (depending on which one you use): yield&amp;#x2F;generators, MutationObserver, postMessage&amp;#x2F;MessageChannel, onreadystatechange, and finally setTimeout (the slowest).&lt;p&gt;IMO the browser vendors should just give in and add setImmediate – if ever there was a need for it, this is it. But maybe they consider ES6 features a good enough substitute, I don&amp;#x27;t know.</text></item></parent_chain><comment><author>pornel</author><text>&amp;gt; IMO the browser vendors should just give in and add setImmediate&lt;p&gt;Firefox has just shipped native promises, and you can use them to polyfill setImmediate instead ;)&lt;p&gt;&lt;a href=&quot;http://kangax.github.io/es5-compat-table/es6/#Promise&quot; rel=&quot;nofollow&quot;&gt;http:&amp;#x2F;&amp;#x2F;kangax.github.io&amp;#x2F;es5-compat-table&amp;#x2F;es6&amp;#x2F;#Promise&lt;/a&gt;</text></comment>