2024-04-25 14:56:13 +03:00
|
|
|
# once
|
|
|
|
|
|
|
|
Creates a function that is restricted to invoking the provided function `func` once.
|
|
|
|
Repeated calls to the function will return the value from the first invocation.
|
|
|
|
|
|
|
|
## Signature
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
function once<F extends () => any>(func: F): F;
|
|
|
|
```
|
|
|
|
|
2024-06-04 11:19:26 +03:00
|
|
|
### Parameters
|
2024-04-25 14:56:13 +03:00
|
|
|
|
|
|
|
- `func` (`F extends () => any`): The function to restrict.
|
|
|
|
|
|
|
|
### Returns
|
|
|
|
|
|
|
|
(`F`): A new function that invokes `func` once and caches the result.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
const initialize = once(() => {
|
|
|
|
console.log('Initialized!');
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
initialize(); // Logs: 'Initialized!' and returns true
|
|
|
|
initialize(); // Returns true without logging
|
|
|
|
```
|