Delay the evaluation of a paramless async function and cache the result (see \href{http://en.wikipedia.org/wiki/Thunk_\%28functional_programming\%29}{\texttt{ thunk}}).


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

\end{DoxyCode}


\href{http://travis-ci.org/mafintosh/thunky}{\texttt{ }}\hypertarget{md_sfcode_node_modules_thunky_README_autotoc_md30389}{}\doxysection{Example}\label{md_sfcode_node_modules_thunky_README_autotoc_md30389}
Let\textquotesingle{}s make a simple function that returns a random number 1 second after it is called for the first time


\begin{DoxyCode}{0}
\DoxyCodeLine{var thunky = require('thunky')}
\DoxyCodeLine{}
\DoxyCodeLine{var test = thunky(function (callback) \{ // the inner function should only accept a callback}
\DoxyCodeLine{  console.log('waiting 1s and returning random number')}
\DoxyCodeLine{  setTimeout(function () \{}
\DoxyCodeLine{    callback(Math.random())}
\DoxyCodeLine{  \}, 1000)}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{test(function (num) \{  // inner function is called the first time we call test}
\DoxyCodeLine{  console.log(num) // prints random number}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{test(function (num) \{  // subsequent calls waits for the first call to finish and return the same value}
\DoxyCodeLine{  console.log(num) // prints the same random number as above}
\DoxyCodeLine{\}) }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_thunky_README_autotoc_md30390}{}\doxysection{Lazy evaluation}\label{md_sfcode_node_modules_thunky_README_autotoc_md30390}
Thunky makes it easy to implement a lazy evaluation pattern.


\begin{DoxyCode}{0}
\DoxyCodeLine{var getDb = thunky(function (callback) \{}
\DoxyCodeLine{  db.open(myConnectionString, callback)}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{var queryDb = function (query, callback) \{}
\DoxyCodeLine{  getDb(function (err, db) \{}
\DoxyCodeLine{    if (err) return callback(err)}
\DoxyCodeLine{    db.query(query, callback)}
\DoxyCodeLine{  \})}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{queryDb('some query', function (err, result) \{ ... \} )}
\DoxyCodeLine{}
\DoxyCodeLine{queryDb('some other query', function (err, result) \{ ... \} ) }

\end{DoxyCode}


The first time {\ttfamily get\+Db} is called it will try do open a connection to the database. Any subsequent calls will just wait for the first call to complete and then call your callback.

A nice property of this pattern is that it {\itshape easily} allows us to pass any error caused by {\ttfamily get\+Db} to the {\ttfamily query\+Db} callback.\hypertarget{md_sfcode_node_modules_thunky_README_autotoc_md30391}{}\doxysection{Error → No caching}\label{md_sfcode_node_modules_thunky_README_autotoc_md30391}
If the thunk callback is called with an {\ttfamily Error} object as the first argument it will not cache the result


\begin{DoxyCode}{0}
\DoxyCodeLine{var fails = thunky(function (callback) \{}
\DoxyCodeLine{  console.log('returning an error')}
\DoxyCodeLine{  callback(new Error('bad stuff'))}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{fails(function (err) \{ // inner function is called}
\DoxyCodeLine{  console.log(err)}
\DoxyCodeLine{\});}
\DoxyCodeLine{}
\DoxyCodeLine{fails(function (err) \{ // inner function is called again as it returned an error before}
\DoxyCodeLine{  console.log(err)}
\DoxyCodeLine{\}) }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_thunky_README_autotoc_md30392}{}\doxysection{Promise version}\label{md_sfcode_node_modules_thunky_README_autotoc_md30392}
A promise version is available as well


\begin{DoxyCode}{0}
\DoxyCodeLine{var thunkyp = require('thunky/promise')}
\DoxyCodeLine{}
\DoxyCodeLine{var ready = thunkyp(async function () \{}
\DoxyCodeLine{  // ... do async stuff}
\DoxyCodeLine{  return 42}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{// same semantics as the callback version}
\DoxyCodeLine{await ready() }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_thunky_README_autotoc_md30393}{}\doxysection{License}\label{md_sfcode_node_modules_thunky_README_autotoc_md30393}
MIT 