
\begin{DoxyCode}{0}
\DoxyCodeLine{npm install immediate -\/-\/save }

\end{DoxyCode}


then


\begin{DoxyCode}{0}
\DoxyCodeLine{var immediate = require("{}immediate"{});}
\DoxyCodeLine{}
\DoxyCodeLine{immediate(function () \{}
\DoxyCodeLine{  // this will run soon}
\DoxyCodeLine{\});}
\DoxyCodeLine{}
\DoxyCodeLine{immediate(function (arg1, arg2) \{}
\DoxyCodeLine{  // get your args like in iojs}
\DoxyCodeLine{\}, thing1, thing2); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_immediate_README_autotoc_md12657}{}\doxysection{Introduction}\label{md_sfcode_node_modules_immediate_README_autotoc_md12657}
{\bfseries{immediate}} is a microtask library, decended from \href{https://github.com/NobleJS/setImmediate}{\texttt{ Noble\+JS\textquotesingle{}s set\+Immediate}}, but including ideas from \href{https://github.com/cujojs/when}{\texttt{ Cujo\textquotesingle{}s When}} and \href{https://github.com/tildeio/rsvp.js}{\texttt{ RSVP}}.

immediate takes the tricks from set\+Immedate and RSVP and combines them with the schedualer inspired (vaugly) by whens.

Note versions 2.\+6.\+5 and earlier were strictly speaking a \textquotesingle{}macrotask\textquotesingle{} library not a microtask one, \href{https://github.com/YuzuJS/setImmediate\#macrotasks-and-microtasks}{\texttt{ see this for the difference}}, if you need a macrotask library, \href{https://github.com/calvinmetcalf/macrotask}{\texttt{ I got you covered}}.

Several new features were added in versions 3.\+1.\+0 and 3.\+2.\+0 to maintain parity with process.\+next\+Tick, but the 3.\+0.\+x series is still being kept up to date if you just need the small barebones version.\hypertarget{md_sfcode_node_modules_immediate_README_autotoc_md12658}{}\doxysection{The Tricks}\label{md_sfcode_node_modules_immediate_README_autotoc_md12658}
\hypertarget{md_sfcode_node_modules_immediate_README_autotoc_md12659}{}\doxysubsection{$<$tt$>$process.\+next\+Tick$<$/tt$>$}\label{md_sfcode_node_modules_immediate_README_autotoc_md12659}
Note that we check for {\itshape actual} Node.\+js environments, not emulated ones like those produced by browserify or similar.\hypertarget{md_sfcode_node_modules_immediate_README_autotoc_md12660}{}\doxysubsection{$<$tt$>$\+Mutation\+Observer$<$/tt$>$}\label{md_sfcode_node_modules_immediate_README_autotoc_md12660}
This is what \href{https://github.com/tildeio/rsvp.js}{\texttt{ RSVP}} uses, it\textquotesingle{}s very fast, details on \href{https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver}{\texttt{ MDN}}.\hypertarget{md_sfcode_node_modules_immediate_README_autotoc_md12661}{}\doxysubsection{$<$tt$>$\+Message\+Channel$<$/tt$>$}\label{md_sfcode_node_modules_immediate_README_autotoc_md12661}
Unfortunately, {\ttfamily post\+Message} has completely different semantics inside web workers, and so cannot be used there. So we turn to \href{http://www.whatwg.org/specs/web-apps/current-work/multipage/web-messaging.html\#channel-messaging}{\texttt{ {\ttfamily Message\+Channel}}}, which has worse browser support, but does work inside a web worker.\hypertarget{md_sfcode_node_modules_immediate_README_autotoc_md12662}{}\doxysubsection{$<$tt$>$$<$script$>$ onreadystatechange$<$/tt$>$}\label{md_sfcode_node_modules_immediate_README_autotoc_md12662}
For our last trick, we pull something out to make things fast in Internet Explorer versions 6 through 8\+: namely, creating a {\ttfamily $<$script$>$} element and firing our calls in its {\ttfamily onreadystatechange} event. This does execute in a future turn of the event loop, and is also faster than {\ttfamily set\+Timeout(…, 0)}, so hey, why not?\hypertarget{md_sfcode_node_modules_immediate_README_autotoc_md12663}{}\doxysection{Tricks we don\textquotesingle{}t use}\label{md_sfcode_node_modules_immediate_README_autotoc_md12663}
\hypertarget{md_sfcode_node_modules_immediate_README_autotoc_md12664}{}\doxysubsection{$<$tt$>$set\+Immediate$<$/tt$>$}\label{md_sfcode_node_modules_immediate_README_autotoc_md12664}
We avoid this process.\+next\+Tick in node is better suited to our needs and in Internet Explorer 10 there is a broken version of set\+Immediate we avoid using this.

In Node.\+js, do


\begin{DoxyCode}{0}
\DoxyCodeLine{npm install immediate }

\end{DoxyCode}


then


\begin{DoxyCode}{0}
\DoxyCodeLine{var immediate = require("{}immediate"{}); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_immediate_README_autotoc_md12665}{}\doxysection{Reference and Reading}\label{md_sfcode_node_modules_immediate_README_autotoc_md12665}

\begin{DoxyItemize}
\item \href{https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/setImmediate/Overview.html}{\texttt{ Efficient Script Yielding W3C Editor\textquotesingle{}s Draft}}
\item \href{http://lists.w3.org/Archives/Public/public-web-perf/2011Jun/0100.html}{\texttt{ W3C mailing list post introducing the specification}}
\item \href{http://ie.microsoft.com/testdrive/Performance/setImmediateSorting/Default.html}{\texttt{ IE Test Drive demo}}
\item \href{http://www.nczonline.net/blog/2011/09/19/script-yielding-with-setimmediate/}{\texttt{ Introductory blog post by Nicholas C. Zakas}}
\item I wrote a couple blog pots on this, \href{http://calvinmetcalf.com/post/61672207151/setimmediate-etc}{\texttt{ part 1}} and \href{http://calvinmetcalf.com/post/61761231881/javascript-schedulers}{\texttt{ part 2}} 
\end{DoxyItemize}