Coder Social home page Coder Social logo

immediate's Introduction

immediate Build Status

testling status

npm install immediate --save

then

var immediate = require("immediate");

immediate(function () {
  // this will run soon
});

immediate(function (arg1, arg2) {
  // get your args like in iojs
}, thing1, thing2);

Introduction

immediate is a microtask library, descended from NobleJS's setImmediate, but including ideas from Cujo's When and RSVP.

immediate takes the tricks from setImmediate and RSVP and combines them with the scheduler inspired (vaguely) by when's.

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.

Several new features were added in versions 3.1.0 and 3.2.0 to maintain parity with process.nextTick, but the 3.0.x series is still being kept up to date if you just need the small barebones version

The Tricks

process.nextTick

Note that we check for actual Node.js environments, not emulated ones like those produced by browserify or similar.

queueMicrotask

Function available in major browser these days which you can use to add a function into the microtask queue managed by V8.

MutationObserver

This is what RSVP uses, it's very fast, details on MDN.

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 using setImmediate because node's process.nextTick is better suited to our needs. Additionally, Internet Explorer 10's implementation of setImmediate is broken.

Reference and Reading

immediate's People

Contributors

calvinmetcalf avatar domenic avatar donavon avatar tinchoz49 avatar guss77 avatar tarqd avatar jphpsf avatar matt-oconnell avatar rubenverborgh avatar whastings avatar yaffle avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.