\begin{quote}
Ensure a function is only called once \end{quote}
When called multiple times it will return the return value from the first call.

{\itshape Unlike the module \href{https://github.com/isaacs/once}{\texttt{ once}}, this one isn\textquotesingle{}t naughty and extending {\ttfamily Function.\+prototype}.}\hypertarget{md_sfcode_node_modules_onetime_readme_autotoc_md19081}{}\doxysection{Install}\label{md_sfcode_node_modules_onetime_readme_autotoc_md19081}

\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm install onetime }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_onetime_readme_autotoc_md19082}{}\doxysection{Usage}\label{md_sfcode_node_modules_onetime_readme_autotoc_md19082}

\begin{DoxyCode}{0}
\DoxyCodeLine{const onetime = require('onetime');}
\DoxyCodeLine{}
\DoxyCodeLine{let i = 0;}
\DoxyCodeLine{}
\DoxyCodeLine{const foo = onetime(() => ++i);}
\DoxyCodeLine{}
\DoxyCodeLine{foo(); //=> 1}
\DoxyCodeLine{foo(); //=> 1}
\DoxyCodeLine{foo(); //=> 1}
\DoxyCodeLine{}
\DoxyCodeLine{onetime.callCount(foo); //=> 3 }

\end{DoxyCode}



\begin{DoxyCode}{0}
\DoxyCodeLine{const onetime = require('onetime');}
\DoxyCodeLine{}
\DoxyCodeLine{const foo = onetime(() => \{\}, \{throw: true\});}
\DoxyCodeLine{}
\DoxyCodeLine{foo();}
\DoxyCodeLine{}
\DoxyCodeLine{foo();}
\DoxyCodeLine{//=> Error: Function `foo` can only be called once }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_onetime_readme_autotoc_md19083}{}\doxysection{API}\label{md_sfcode_node_modules_onetime_readme_autotoc_md19083}
\hypertarget{md_sfcode_node_modules_onetime_readme_autotoc_md19084}{}\doxysubsection{onetime(fn, options?)}\label{md_sfcode_node_modules_onetime_readme_autotoc_md19084}
Returns a function that only calls {\ttfamily fn} once.\hypertarget{md_sfcode_node_modules_onetime_readme_autotoc_md19085}{}\doxysubsubsection{fn}\label{md_sfcode_node_modules_onetime_readme_autotoc_md19085}
Type\+: {\ttfamily Function}

Function that should only be called once.\hypertarget{md_sfcode_node_modules_onetime_readme_autotoc_md19086}{}\doxysubsubsection{options}\label{md_sfcode_node_modules_onetime_readme_autotoc_md19086}
Type\+: {\ttfamily object}\hypertarget{md_sfcode_node_modules_onetime_readme_autotoc_md19087}{}\doxyparagraph{throw}\label{md_sfcode_node_modules_onetime_readme_autotoc_md19087}
Type\+: {\ttfamily boolean}\textbackslash{} Default\+: {\ttfamily false}

Throw an error when called more than once.\hypertarget{md_sfcode_node_modules_onetime_readme_autotoc_md19088}{}\doxysubsection{onetime.\+call\+Count(fn)}\label{md_sfcode_node_modules_onetime_readme_autotoc_md19088}
Returns a number representing how many times {\ttfamily fn} has been called.

Note\+: It throws an error if you pass in a function that is not wrapped by {\ttfamily onetime}.


\begin{DoxyCode}{0}
\DoxyCodeLine{const onetime = require('onetime');}
\DoxyCodeLine{}
\DoxyCodeLine{const foo = onetime(() => \{\});}
\DoxyCodeLine{}
\DoxyCodeLine{foo();}
\DoxyCodeLine{foo();}
\DoxyCodeLine{foo();}
\DoxyCodeLine{}
\DoxyCodeLine{console.log(onetime.callCount(foo));}
\DoxyCodeLine{//=> 3 }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_onetime_readme_autotoc_md19089}{}\doxysubsubsection{fn}\label{md_sfcode_node_modules_onetime_readme_autotoc_md19089}
Type\+: {\ttfamily Function}

Function to get call count from.\hypertarget{md_sfcode_node_modules_onetime_readme_autotoc_md19090}{}\doxysection{onetime for enterprise}\label{md_sfcode_node_modules_onetime_readme_autotoc_md19090}
Available as part of the Tidelift Subscription.

The maintainers of onetime and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. \href{https://tidelift.com/subscription/pkg/npm-onetime?utm_source=npm-onetime&utm_medium=referral&utm_campaign=enterprise&utm_term=repo}{\texttt{ Learn more.}} 