once-only
v1.0.3
Published
Call function once. Create a never to be repeated function
Downloads
4
Maintainers
Readme
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
import { onceOnly } from 'once-only'
Create:
const createUserOnce = onceOnly(createUser);
Original function will be called only once. It returns cache on a repeated call:
const currentUser = createUserOnce({ userId: 234 });
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:
onceOnly(fetchCompany, { attach: true });
Then call it:
const promise = fetchCompany.once({ companyId: 11 });
strict
option
You can throw an error when a function is called more than once:
const getIdOnceOnly = onceOnly(getId, { strict: true });
const users = getIdOnceOnly({ userId: 234 }); // 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.log(fetchUsersOnce.called); // true or false
.cache
console.log(fetchUserOnce.cache); // function: () => cache
const result = fetchUserOnce.cache(); // call cache getter
console.log(result); // 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