1# immediate [](https://travis-ci.org/calvinmetcalf/immediate)
4npm install immediate --save
10var immediate = require("immediate");
12immediate(function () {
16immediate(function (arg1, arg2) {
17 // get your args like in iojs
23**immediate** is a microtask library, decended from [NobleJS's setImmediate](https://github.com/NobleJS/setImmediate), but including ideas from [Cujo's When](https://github.com/cujojs/when) and [RSVP][RSVP].
25immediate takes the tricks from setImmedate and RSVP and combines them with the schedualer inspired (vaugly) by whens.
27Note versions 2.6.5 and earlier were strictly speaking a 'macrotask' library not a microtask one, [see this for the difference](https://github.com/YuzuJS/setImmediate#macrotasks-and-microtasks), if you need a macrotask library, [I got you covered](https://github.com/calvinmetcalf/macrotask).
29Several new features were added in versions 3.1.0 and 3.2.0 to maintain parity with
30process.nextTick, but the 3.0.x series is still being kept up to date if you just need
31the small barebones version.
38Note that we check for *actual* Node.js environments, not emulated ones like those produced by browserify or similar.
42This is what [RSVP][RSVP] uses, it's very fast, details on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver).
47Unfortunately, `postMessage` has completely different semantics inside web workers, and so cannot be used there. So we
48turn to [`MessageChannel`][MessageChannel], which has worse browser support, but does work inside a web worker.
50### `<script> onreadystatechange`
52For our last trick, we pull something out to make things fast in Internet Explorer versions 6 through 8: namely,
53creating a `<script>` element and firing our calls in its `onreadystatechange` event. This does execute in a future
54turn of the event loop, and is also faster than `setTimeout(…, 0)`, so hey, why not?
59We 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.
71var immediate = require("immediate");
75## Reference and Reading
77 * [Efficient Script Yielding W3C Editor's Draft][spec]
78 * [W3C mailing list post introducing the specification][list-post]
79 * [IE Test Drive demo][ie-demo]
80 * [Introductory blog post by Nicholas C. Zakas][ncz]
81 * I wrote a couple blog pots on this, [part 1][my-blog-1] and [part 2][my-blog-2]
83[RSVP]: https://github.com/tildeio/rsvp.js
84[spec]: https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/setImmediate/Overview.html
85[list-post]: http://lists.w3.org/Archives/Public/public-web-perf/2011Jun/0100.html
86[ie-demo]: http://ie.microsoft.com/testdrive/Performance/setImmediateSorting/Default.html
87[ncz]: http://www.nczonline.net/blog/2011/09/19/script-yielding-with-setimmediate/
88[nextTick]: http://nodejs.org/docs/v0.8.16/api/process.html#process_process_nexttick_callback
89[postMessage]: http://www.whatwg.org/specs/web-apps/current-work/multipage/web-messaging.html#posting-messages
90[MessageChannel]: http://www.whatwg.org/specs/web-apps/current-work/multipage/web-messaging.html#channel-messaging
91[cross-browser-demo]: http://calvinmetcalf.github.io/setImmediate-shim-demo
92[my-blog-1]:http://calvinmetcalf.com/post/61672207151/setimmediate-etc
93[my-blog-2]:http://calvinmetcalf.com/post/61761231881/javascript-schedulers
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list