once
A function that ensures the given callback is executed only once. After the first call, it returns the previously computed result.
Code
Interface
typescript
function once<T extends (...args: any[]) => any>(callback: T): T
Usage
Basic Usage
typescript
import { once } from '@modern-kit/utils';
const initialize = once(() => {
console.log('Initialization complete');
return true;
});
initialize(); // Logs 'Initialization complete' and returns true.
initialize(); // Nothing is logged, and the previous result true is returned.
With Arguments
typescript
import { once } from '@modern-kit/utils';
const onceSum = once((a: number, b: number) => {
return a + b;
});
onceSum(2, 3); // 5
onceSum(3, 10); // 5, subsequent calls return the previous result even if arguments change.