Callback wrapping utility\hypertarget{md_sfcode_node_modules_wrappy_README_autotoc_md34232}{}\doxysection{USAGE}\label{md_sfcode_node_modules_wrappy_README_autotoc_md34232}

\begin{DoxyCode}{0}
\DoxyCodeLine{var wrappy = require("{}wrappy"{})}
\DoxyCodeLine{}
\DoxyCodeLine{// var wrapper = wrappy(wrapperFunction)}
\DoxyCodeLine{}
\DoxyCodeLine{// make sure a cb is called only once}
\DoxyCodeLine{// See also: http://npm.im/once for this specific use case}
\DoxyCodeLine{var once = wrappy(function (cb) \{}
\DoxyCodeLine{  var called = false}
\DoxyCodeLine{  return function () \{}
\DoxyCodeLine{    if (called) return}
\DoxyCodeLine{    called = true}
\DoxyCodeLine{    return cb.apply(this, arguments)}
\DoxyCodeLine{  \}}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{function printBoo () \{}
\DoxyCodeLine{  console.log('boo')}
\DoxyCodeLine{\}}
\DoxyCodeLine{// has some rando property}
\DoxyCodeLine{printBoo.iAmBooPrinter = true}
\DoxyCodeLine{}
\DoxyCodeLine{var onlyPrintOnce = once(printBoo)}
\DoxyCodeLine{}
\DoxyCodeLine{onlyPrintOnce() // prints 'boo'}
\DoxyCodeLine{onlyPrintOnce() // does nothing}
\DoxyCodeLine{}
\DoxyCodeLine{// random property is retained!}
\DoxyCodeLine{assert.equal(onlyPrintOnce.iAmBooPrinter, true) }

\end{DoxyCode}
 