\href{http://badge.fury.io/js/run-async}{\texttt{ }} \href{http://travis-ci.org/SBoudrias/run-async}{\texttt{ }} \href{https://david-dm.org/SBoudrias/run-async}{\texttt{ }}

Utility method to run a function either synchronously or asynchronously using a series of common patterns. This is useful for library author accepting sync or async functions as parameter. {\ttfamily run\+Async} will always run them as an async method, and normalize the multiple signature.\hypertarget{md_sfcode_node_modules_run_async_README_autotoc_md25956}{}\doxysection{Installation}\label{md_sfcode_node_modules_run_async_README_autotoc_md25956}

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

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_run_async_README_autotoc_md25957}{}\doxysection{Usage}\label{md_sfcode_node_modules_run_async_README_autotoc_md25957}
Here\textquotesingle{}s a simple example print the function results and three options a user can provide a function.


\begin{DoxyCode}{0}
\DoxyCodeLine{var runAsync = require('run-\/async');}
\DoxyCodeLine{}
\DoxyCodeLine{var printAfter = function (func) \{}
\DoxyCodeLine{  var cb = function (err, returnValue) \{}
\DoxyCodeLine{    console.log(returnValue);}
\DoxyCodeLine{  \};}
\DoxyCodeLine{  runAsync(func, cb)(/* arguments for func */);}
\DoxyCodeLine{\}; }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_run_async_README_autotoc_md25958}{}\doxyparagraph{Using $<$tt$>$this.\+async$<$/tt$>$}\label{md_sfcode_node_modules_run_async_README_autotoc_md25958}

\begin{DoxyCode}{0}
\DoxyCodeLine{printAfter(function () \{}
\DoxyCodeLine{  var done = this.async();}
\DoxyCodeLine{}
\DoxyCodeLine{  setTimeout(function () \{}
\DoxyCodeLine{    done(null, 'done running with callback');}
\DoxyCodeLine{  \}, 10);}
\DoxyCodeLine{\}); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_run_async_README_autotoc_md25959}{}\doxyparagraph{Returning a promise}\label{md_sfcode_node_modules_run_async_README_autotoc_md25959}

\begin{DoxyCode}{0}
\DoxyCodeLine{printAfter(function () \{}
\DoxyCodeLine{  return new Promise(function (resolve, reject) \{}
\DoxyCodeLine{    resolve('done running with promises');}
\DoxyCodeLine{  \});}
\DoxyCodeLine{\}); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_run_async_README_autotoc_md25960}{}\doxyparagraph{Synchronous function}\label{md_sfcode_node_modules_run_async_README_autotoc_md25960}

\begin{DoxyCode}{0}
\DoxyCodeLine{printAfter(function () \{}
\DoxyCodeLine{  return 'done running sync function';}
\DoxyCodeLine{\}); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_run_async_README_autotoc_md25961}{}\doxysubsubsection{run\+Async.\+cb}\label{md_sfcode_node_modules_run_async_README_autotoc_md25961}
{\ttfamily run\+Async.\+cb} supports all the function types that {\ttfamily run\+Async} does and additionally a traditional {\bfseries{callback as the last argument}} signature\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var runAsync = require('run-\/async');}
\DoxyCodeLine{}
\DoxyCodeLine{// IMPORTANT: The wrapped function must have a fixed number of parameters.}
\DoxyCodeLine{runAsync.cb(function(a, b, cb) \{}
\DoxyCodeLine{  cb(null, a + b);}
\DoxyCodeLine{\}, function(err, result) \{}
\DoxyCodeLine{  console.log(result)}
\DoxyCodeLine{\})(1, 2) }

\end{DoxyCode}


If your version of node support Promises natively (node $>$= 0.\+12), {\ttfamily run\+Async} will return a promise. Example\+: {\ttfamily run\+Async(func)(arg1, arg2).then(cb)}\hypertarget{md_sfcode_node_modules_run_async_README_autotoc_md25962}{}\doxysection{Licence}\label{md_sfcode_node_modules_run_async_README_autotoc_md25962}
Copyright (c) 2014 Simon Boudrias (twitter\+: @vaxilart) ~\newline
 Licensed under the MIT license. 