Ensures a function is executed only once, caching and returning the result for subsequent calls.
ResolveOnce automatically detects whether the function returns a synchronous value or a Promise,
and handles both cases appropriately. All subsequent calls will receive the same cached result.
Supports optional context parameter and can be reset to allow re-execution.
Template: T
The return type of the function (can be synchronous or Promise)
Template: CTX
Optional context type passed to the function
Example
constexpensiveOp = newResolveOnce<number>();
// First call executes the function constresult1 = expensiveOp.once(() =>computeExpensiveValue());
// Subsequent calls return cached result constresult2 = expensiveOp.once(() =>computeExpensiveValue()); // Not executed
// Reset to allow re-execution expensiveOp.reset();
Ensures a function is executed only once, caching and returning the result for subsequent calls.
ResolveOnce automatically detects whether the function returns a synchronous value or a Promise, and handles both cases appropriately. All subsequent calls will receive the same cached result. Supports optional context parameter and can be reset to allow re-execution.
Template: T
The return type of the function (can be synchronous or Promise)
Template: CTX
Optional context type passed to the function
Example