https://github.com/fabiospampinato/function-once
Wraps a function so that it's only ever executed once.
https://github.com/fabiospampinato/function-once
once
Last synced: 2 months ago
JSON representation
Wraps a function so that it's only ever executed once.
- Host: GitHub
- URL: https://github.com/fabiospampinato/function-once
- Owner: fabiospampinato
- License: mit
- Created: 2021-12-28T00:29:41.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2025-01-17T00:28:25.000Z (9 months ago)
- Last Synced: 2025-07-02T03:48:41.700Z (3 months ago)
- Topics: once
- Language: JavaScript
- Homepage:
- Size: 9.77 KB
- Stars: 8
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# Function Once
Wraps a function so that it's only ever executed once.
Note: `this` is always set to `undefined` and only functions that don't accept any arguments are supported, as using those with a function that's only ever executed once is practically just a footgun. If you need different `this` or different arguments you should probably use memoization instead.
## Install
```sh
npm install function-once
```## Usage
```ts
import once from 'function-once';// Let's make sure a function is at most executed only once
const rand = once (() => Math.random ());
rand (); // => 0.3344627371267874
rand (); // => 0.3344627371267874
```## License
MIT © Fabio Spampinato