Core
Func<R> is a zero-argument async function wrapper. It turns any Future<R> Function() into an object that can carry decorators.
Core
The core layer provides the wrapper classes that every Funx decorator extends. Understanding these wrappers is essential before chaining decorators.
Func
What it is
Func<R> is a zero-argument async function wrapper. It turns any Future<R> Function() into an object that can carry decorators.
When to use it
- You have an async operation with no inputs (e.g., load config, refresh token, fetch current user).
- You want to apply timing, concurrency, or reliability decorators.
Async / sync support
Func<R> is async-only. For sync equivalents use FuncSync<R>.
API reference
| |
All async decorators are available on Func<R> unless noted otherwise.
Examples
Minimal
| |
Real world
| |
Best practices
- Use
Funcfor operations whose inputs can be captured by closure rather than parameters. - Combine
Funcwithmemoizeoroncefor expensive initialization.
Common pitfalls
Funcis not aFunction; you must call it withawait func().- Chaining order matters:
timeout(retry(...))retries on timeout;retry(timeout(...))times out each retry attempt.
Func1
What it is
Func1<T, R> wraps a single-argument async function: Future<R> Function(T).
When to use it
- The operation depends on one runtime argument (e.g., fetch user by ID, validate email).
- You need arity-specific decorators such as
validate,batch, orqueue.
Async / sync support
Async-only. Sync equivalent: FuncSync1<T, R>.
API reference
| |
Examples
Minimal
| |
Real world
| |
Best practices
- Prefer
Func1overFuncwhen the input must be passed at call time. - Use
Func1for queue, batch, and backpressure decorators, which are arity-specific.
Common pitfalls
Func1decorators that rely on argument identity (e.g.,memoize,deduplicate) require stable==andhashCodefor complex types.
Func2
What it is
Func2<T1, T2, R> wraps a two-argument async function: Future<R> Function(T1, T2).
When to use it
- The operation naturally takes two arguments (e.g., transfer money from A to B, query by ID and limit).
Async / sync support
Async-only. Sync equivalent: FuncSync2<T1, T2, R>.
API reference
| |
Examples
Minimal
| |
Real world
| |
Best practices
- Use
Func2when both arguments are required and independent. - For more than two arguments, consider a data class and
Func1.
FuncSync, FuncSync1, FuncSync2
What it is
Synchronous wrappers that mirror the async API but return values immediately.
When to use it
- You have pure synchronous logic that still benefits from the Funx API shape.
- You need scheduling decorators (
schedule,scheduleRecurring,scheduleCustom), the only decorators available on sync wrappers.
Async / sync support
Sync-only. No timing, concurrency, or reliability decorators are supported except schedule() on FuncSync<R>.
API reference
| |
Examples
Minimal
| |
Real world
| |
Best practices
- Use sync wrappers for deterministic, non-blocking computations.
- Do not wrap I/O in sync wrappers and then schedule them; prefer async wrappers for I/O.
Common pitfalls
- Calling a
FuncSyncfrom an async context works, but you cannot await it meaningfully. - Most decorators silently do not exist on sync wrappers; trying to chain them is a compile-time error.
Plain function extensions
What it is
Extension methods that apply stateless Funx decorators directly to plain async
functions (Future<R> Function(), Future<R> Function(T), and
Future<R> Function(T1, T2)) without wrapping them in Func, Func1, or
Func2 first.
When to use it
- You want the most ergonomic API and do not need stateful decorators.
- You are passing functions to libraries that expect plain function types.
Stateful decorators
Stateful decorators such as debounce, throttle, circuitBreaker,
memoize, monitorObservability, and audit remain Func-only. Use the
wrappers above when you need them.
Examples
0-argument function
| |
1-argument function
| |
2-argument function
| |
Future extensions
What it is
Convenience decorators for Future<T> values.
When to use it
- You already have a future and want to add a timeout or fallback without
wrapping the operation in a
Func.
Examples
| |
Type aliases
Funx also exports type aliases for plain functions:
| |
Use these when you need to declare a function signature without wrapping it in a class.