Skip to content

Fallback

Arnel Robles edited this page Sep 29, 2026 · 1 revision

Fallback (Pocket)

Package: Carom.Extensions. Extension methods on Func<T> and Func<Task<T>>.

using Carom.Extensions;

// A constant
var config = await new Func<Task<AppConfig>>(() => configService.LoadAsync())
    .PocketAsync(AppConfig.Default);

// Computed from the exception
var price = new Func<decimal>(() => pricing.Get(sku))
    .Pocket(ex => cache.LastKnownPrice(sku));

// Retry first, then fall back
var rates = await CaromFallbackExtensions.ShotWithPocketAsync(
    () => fx.GetRatesAsync(), fallback: Rates.Empty, retries: 3);

Cancellation

The async overloads take a CancellationToken. When that token is cancelled the OperationCanceledException is rethrown, not replaced with the fallback, so a caller who cancels is not handed a default value as if the call had worked.

The synchronous Pocket overloads have no token and fall back on any exception, cancellation included.

Inside a pipeline

For a typed fallback in a named pipeline, use AddFallback<TResult> on the DI pipeline. It only applies when the pipeline's result type is TResult.

Clone this wiki locally