Call function once
Create a once-only function and it will be never repeated
Installation
npm install once-only
or
yarn add once-only
Usage
Create:
const createUserOnce = ;
Original function will be called only once. It returns cache on a repeated call:
const currentUser = ;
You can pass configuration object to onceOnly
as a second argument:
API
Configuration options:
You can pass config as a second argument.
Default values are: { attach: false, strict: false }
attach
option
Attach once
function to the original function:
;
Then call it:
const promise = fetchCompany;
strict
option
You can throw an error when a function is called more than once:
const getIdOnceOnly = ;const users = ; // Throws an error
Static properties of a once-only function:
Returned function has two static properties (as well as an attached once
function):
.called
console; // true or false
.cache
console; // function: () => cacheconst result = fetchUserOnce; // call cache getterconsole; // logs the result of the first function call
Tip
If you found this packeage useful, please star it on GitHub ★
Definition of "once-only" in English
once-only adjective
That occurs, operates, etc., on one occasion only.
Origin:
1960s; earliest use found in The Sunday Express.
once-only in British adjective
(ˌwʌnsˈəʊnlɪ)
never to be repeated
Examples:
- This is a once-only offer.
- This once-only opportunity to select one of these books absolutely free
- Membership is valid for ten years for a once-only donation of £2,500.
^Collins English Dictionary. Copyright © HarperCollins Publishers
Author
@doasync
Licence
MIT