On this page
article
Lazy Loading
Lazy loading defers execution until the result is first requested. This is the default behavior for most wrapped functions, but the explicit decorator can be combined with others for clarity.
Lazy Loading
What it is
Lazy loading defers execution until the result is first requested. This is the default behavior for most wrapped functions, but the explicit decorator can be combined with others for clarity.
When to use it
- Expensive resources that may never be needed.
- Avoiding startup cost for optional features.
- Composing with warm up or memoize to control when work happens.
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
- Combine
lazywithmemoizeto compute once on demand and cache forever. - Use
lazyinstead ofwarmUpwhen the resource may not be needed. - Be aware that the first call pays the full cost.
Common pitfalls
- First-call latency: Lazy loading shifts cost to the first request, which can surprise callers.
- Not a separate future per call: Like
once, lazy caches the first result.