Skip to content

[Proposal]: once function #90

@EhsanKey

Description

@EhsanKey

Function Signature

function once<T extends (...args: any[]) => any>(fn: T): T;

Motivation

once restricts a function to be invoked only once. Subsequent calls return the cached result:

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.

Metadata

Metadata

Labels

proposalNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions