CoolFace
Apppublic

TrinetraLabs/Placebo_AI

sourceHugging Faceupdated 2mo agoView on Hugging Face
0likes
README.md94 linesDownload Raw Back to immediate
1# immediate [![Build Status](https://travis-ci.org/calvinmetcalf/immediate.svg?branch=master)](https://travis-ci.org/calvinmetcalf/immediate)2 3```4npm install immediate --save5```6 7then8 9```js10var immediate = require("immediate");11 12immediate(function () {13  // this will run soon14});15 16immediate(function (arg1, arg2) {17  // get your args like in iojs18}, thing1, thing2);19```20 21## Introduction22 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].24 25immediate takes the tricks from setImmedate and RSVP and combines them with the schedualer inspired (vaugly) by whens.26 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).28 29Several new features were added in versions 3.1.0 and 3.2.0 to maintain parity with30process.nextTick, but the 3.0.x series is still being kept up to date if you just need31the small barebones version.32 33 34## The Tricks35 36### `process.nextTick`37 38Note that we check for *actual* Node.js environments, not emulated ones like those produced by browserify or similar.39 40### `MutationObserver`41 42This is what [RSVP][RSVP] uses, it's very fast, details on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver).43 44 45### `MessageChannel`46 47Unfortunately, `postMessage` has completely different semantics inside web workers, and so cannot be used there. So we48turn to [`MessageChannel`][MessageChannel], which has worse browser support, but does work inside a web worker.49 50### `<script> onreadystatechange`51 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 future54turn of the event loop, and is also faster than `setTimeout(…, 0)`, so hey, why not?55 56## Tricks we don't use57 58### `setImmediate`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.60 61 62In Node.js, do63 64```65npm install immediate66```67 68then69 70```js71var immediate = require("immediate");72```73 74 75## Reference and Reading76 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]82 83[RSVP]: https://github.com/tildeio/rsvp.js84[spec]: https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/setImmediate/Overview.html85[list-post]: http://lists.w3.org/Archives/Public/public-web-perf/2011Jun/0100.html86[ie-demo]: http://ie.microsoft.com/testdrive/Performance/setImmediateSorting/Default.html87[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_callback89[postMessage]: http://www.whatwg.org/specs/web-apps/current-work/multipage/web-messaging.html#posting-messages90[MessageChannel]: http://www.whatwg.org/specs/web-apps/current-work/multipage/web-messaging.html#channel-messaging91[cross-browser-demo]: http://calvinmetcalf.github.io/setImmediate-shim-demo92[my-blog-1]:http://calvinmetcalf.com/post/61672207151/setimmediate-etc93[my-blog-2]:http://calvinmetcalf.com/post/61761231881/javascript-schedulers94