Package Exports
- immediate
- immediate/lib/fakeNextTick.js
- immediate/lib/nextTick.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (immediate) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
immediate 
Introduction
immediate.js is a microtask library, based on NobleJS's setImmediate, but stealing the best ideas from Cujo's When and RSVP.
immediate takes the tricks from setImmedate and RSVP and combines them with the schedualer from when.
Note versions 2.6.5 and earlier were strictly speaking a 'macrotask' library not a microtask one, see this for the difference, if you need a macrotask library, I got you covered.
The Tricks
process.nextTick
Note that we check for actual Node.js environments, not emulated ones like those produced by browserify or similar.
Such emulated environments often already include a process.nextTick shim that's not as browser-compatible as
setImmediate.js.
MutationObserver
This is what RSVP uses, it's very fast, details on MDN
postMessage
In Firefox 3+, Internet Explorer 9+, all modern WebKit browsers, and Opera 9.5+, postMessage is
available and provides a good way to queue tasks on the event loop. It's quite the abuse, using a cross-document
messaging protocol within the same document simply to get access to the event loop task queue, but until there are
native implementations, this is the best option.
Note that Internet Explorer 8 includes a synchronous version of postMessage. We detect this, or any other such
synchronous implementation, and fall back to another trick.
Also note that Internet Explorer 10 has a bug relating to refusing to yield the queue which effects setImmediate, postMessage and MessageChannel
MessageChannel
Unfortunately, postMessage has completely different semantics inside web workers, and so cannot be used there. So we
turn to MessageChannel, which has worse browser support, but does work inside a web worker.
<script> onreadystatechange
For our last trick, we pull something out to make things fast in Internet Explorer versions 6 through 8: namely,
creating a <script> element and firing our calls in its onreadystatechange event. This does execute in a future
turn of the event loop, and is also faster than setTimeout(…, 0), so hey, why not?
Tricks we don't use
setImmediate
We avoid this process.nextTick in node is better suited to our needs and in Internet Explorer 10 there is a broken version of setImmediate we avoid using this.
Usage
In the browser, include it with a <script> tag; pretty simple. Creates a global
called immediate which should act like setImmediate. It also has a method called
clear which should act like clearImmediate.
In Node.js, do
npm install immediatethen
var immediate = require("immediate");somewhere early in your app; it attaches to the global.
Reference and Reading
- Efficient Script Yielding W3C Editor's Draft
- W3C mailing list post introducing the specification
- IE Test Drive demo
- Introductory blog post by Nicholas C. Zakas
- I wrote a couple blog pots on this, part 1 and part 2
