fullstacksjs / fullstacksjs/toolbox

[Proposal]: once function

Open
#90 0 comments 0 reactions 2 assignees Claimed by @ASafaeirad View on GitHub
proposal
Dominant language
TypeScript
Stars
73
Forks
18
Avg merge
21h 5m
Merged PRs (30d)
5

Description

### Function Signature

function once any>(fn: T): T;

### Motivation

once restricts a function to be invoked only once. Subsequent calls return the cached result:
```ts
const initialize = once(() => {
console.log('Loading...');
return { ready: true };
});

initialize(); // Logs: 'Loading...', returns { ready: true }
initialize(); // No log, returns { ready: true }
```

Benefits:
- Useful for expensive operations like initialization or API calls.
- Ensures idempotency for setup functions.
- Simple and lightweight memoization for single invocation.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.