Home
Docs
GitHub
Pricing
Blog
Log In

Run Sandworm Audit for your App

Get started
Hold on, we're currently generating a fresh version of this report
Generated on Apr 9, 2024 via pnpm

onetime 5.1.2

Ensure a function is only called once
Package summary
Share
0
issues
1
license
2
MIT
Package created
13 Dec 2013
Version published
9 Aug 2020
Maintainers
1
Total deps
2
Direct deps
1
License
MIT

Issues

0
This package has no issues

Licenses

MIT License

Permissive
OSI Approved
This is a human-readable summary of (and not a substitute for) the license. Disclaimer.
Can
commercial-use
modify
distribute
sublicense
private-use
Cannot
hold-liable
Must
include-copyright
include-license
2 Packages, Including:
mimic-fn@2.1.0
onetime@5.1.2
Disclaimer

This deed highlights only some of the key features and terms of the actual license. It is not a license and has no legal value. You should carefully review all of the terms and conditions of the actual license before using the licensed material.

Sandworm is not a law firm and does not provide legal services. Distributing, displaying, or linking to this deed or the license that it summarizes does not create a lawyer-client or any other relationship.

Direct Dependencies

1
All Dependencies CSV
β“˜ This is a list of onetime 's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.
NameVersionSizeLicenseTypeVulnerabilities
mimic-fn2.1.02.07 kBMIT
prod

Visualizations

Frequently Asked Questions

What does onetime do?

The "onetime" npm package is designed to ensure that a particular function in your JavaScript code is only called once. When the function wrapped by "onetime" is called multiple times, it returns the return value from the first call, effectively enforcing its execution once. This can be useful in avoiding recurring calls to the same function, saving computational resources and helping to maintain cleaner, more efficient code.

How do you use onetime?

Installing and using "onetime" is straightforward. Firstly, you'll need to add it to your project with npm:

$ npm install onetime

Here is a basic example of how to use "onetime":

import onetime from 'onetime';

let index = 0;

const foo = onetime(() => ++index);

foo(); //=> 1
foo(); //=> 1
foo(); //=> 1

onetime.callCount(foo); //=> 3

In this case, the foo function increments the index variable. However, despite foo being called thrice, the resulting value is '1', proving that the function only actually executed once. Then, the onetime.callCount(foo) checks how many times foo has been called, returning '3'.

You can also opt to throw an error when a function is called more than once:

import onetime from 'onetime';

const foo = onetime(() => {}, {throw: true});

foo();

foo();
//=> Error: Function `foo` can only be called once

In this case, calling foo a second time throws an error, making it explicit when the function is mistakenly called more than once.

Where are the onetime docs?

The documentation for the "onetime" npm package can be found within its README file on the GitHub repository. The README file provides a comprehensive description of the functionality, including the API methods onetime(fn, options?) and onetime.callCount(fn). It also provides usage examples for learning purposes. You may access the official docs here. Make sure to fully read the documentation to understand all the capabilities of the "onetime" npm package.