On this page
article
Once
Once guarantees that the wrapped function executes at most one time. All subsequent calls return the cached result.
Once
What it is
Once guarantees that the wrapped function executes at most one time. All subsequent calls return the cached result.
When to use it
- Singleton initialization.
- Feature flags or configuration loaded once per app lifetime.
- One-time analytics setup.
Async / sync support
| Wrapper | Support |
|---|---|
Func<R> | ✅ Async |
Func1<T, R> | ✅ Async |
Func2<T1, T2, R> | ✅ Async |
FuncSync<R> | ❌ No |
API reference
| |
No parameters.
Examples
Basic example
| |
Real-world example
| |
Best practices
- Use
oncefor side effects that should happen exactly one time. - Pair with
fallbackif initialization failures should be recoverable. - Keep the once-wrapped function deterministic.
Common pitfalls
- Failed once is still once: If the single execution throws, the wrapper remembers the failure and rethrows on subsequent calls. Combine with
retryorfallbackto handle this. - Arguments ignored after first call: For
Func1/Func2, only the first invocation’s arguments are used.