Only call a function once.\hypertarget{md_sfcode_node_modules_once_README_autotoc_md19078}{}\doxysection{usage}\label{md_sfcode_node_modules_once_README_autotoc_md19078}

\begin{DoxyCode}{0}
\DoxyCodeLine{var once = require('once')}
\DoxyCodeLine{}
\DoxyCodeLine{function load (file, cb) \{}
\DoxyCodeLine{  cb = once(cb)}
\DoxyCodeLine{  loader.load('file')}
\DoxyCodeLine{  loader.once('load', cb)}
\DoxyCodeLine{  loader.once('error', cb)}
\DoxyCodeLine{\} }

\end{DoxyCode}


Or add to the Function.\+prototype in a responsible way\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{// only has to be done once}
\DoxyCodeLine{require('once').proto()}
\DoxyCodeLine{}
\DoxyCodeLine{function load (file, cb) \{}
\DoxyCodeLine{  cb = cb.once()}
\DoxyCodeLine{  loader.load('file')}
\DoxyCodeLine{  loader.once('load', cb)}
\DoxyCodeLine{  loader.once('error', cb)}
\DoxyCodeLine{\} }

\end{DoxyCode}


Ironically, the prototype feature makes this module twice as complicated as necessary.

To check whether you function has been called, use {\ttfamily fn.\+called}. Once the function is called for the first time the return value of the original function is saved in {\ttfamily fn.\+value} and subsequent calls will continue to return this value.


\begin{DoxyCode}{0}
\DoxyCodeLine{var once = require('once')}
\DoxyCodeLine{}
\DoxyCodeLine{function load (cb) \{}
\DoxyCodeLine{  cb = once(cb)}
\DoxyCodeLine{  var stream = createStream()}
\DoxyCodeLine{  stream.once('data', cb)}
\DoxyCodeLine{  stream.once('end', function () \{}
\DoxyCodeLine{    if (!cb.called) cb(new Error('not found'))}
\DoxyCodeLine{  \})}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_once_README_autotoc_md19079}{}\doxysection{$<$tt$>$once.\+strict(func)$<$/tt$>$}\label{md_sfcode_node_modules_once_README_autotoc_md19079}
Throw an error if the function is called twice.

Some functions are expected to be called only once. Using {\ttfamily once} for them would potentially hide logical errors.

In the example below, the {\ttfamily greet} function has to call the callback only once\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{function greet (name, cb) \{}
\DoxyCodeLine{  // return is missing from the if statement}
\DoxyCodeLine{  // when no name is passed, the callback is called twice}
\DoxyCodeLine{  if (!name) cb('Hello anonymous')}
\DoxyCodeLine{  cb('Hello ' + name)}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{function log (msg) \{}
\DoxyCodeLine{  console.log(msg)}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{// this will print 'Hello anonymous' but the logical error will be missed}
\DoxyCodeLine{greet(null, once(msg))}
\DoxyCodeLine{}
\DoxyCodeLine{// once.strict will print 'Hello anonymous' and throw an error when the callback will be called the second time}
\DoxyCodeLine{greet(null, once.strict(msg)) }

\end{DoxyCode}
 