Skip to main content

any

Macro any 

Source
macro_rules! any {
    ($ctx:expr, $($fut:expr),+ $(,)?) => { ... };
}
Expand description

Returns the first successful result from multiple futures.

This macro boxes each future to enable combining heterogeneous future types, then delegates to ctx.any(). All futures run concurrently, and the macro returns as soon as any future succeeds. If all futures fail, returns a combined error.

§Arguments

  • $ctx - The DurableContext instance
  • $fut - One or more future expressions (comma-separated)

§Returns

  • Ok(T) - The first successful result (remaining futures are cancelled)
  • Err(DurableError) - Combined error if all futures fail

§Examples

§Basic Fallback Pattern

use durable_execution_sdk::any;

// Clone contexts for each future
let ctx1 = ctx.clone();
let ctx2 = ctx.clone();
let ctx3 = ctx.clone();

// Try multiple sources, return first success
let data = any!(ctx,
    async move { ctx1.step(|_| fetch_from_primary(), None).await },
    async move { ctx2.step(|_| fetch_from_secondary(), None).await },
    async move { ctx3.step(|_| fetch_from_cache(), None).await },
).await?;

§Redundant Service Calls

use durable_execution_sdk::any;

let ctx1 = ctx.clone();
let ctx2 = ctx.clone();

// Call multiple redundant services, use first response
let price = any!(ctx,
    async move { ctx1.step(|_| get_price_from_service_a(item_id), None).await },
    async move { ctx2.step(|_| get_price_from_service_b(item_id), None).await },
).await?;

§Handling All Failures

use durable_execution_sdk::any;

let ctx1 = ctx.clone();
let ctx2 = ctx.clone();

// If all sources fail, get combined error
let result = any!(ctx,
    async move { ctx1.step(|_| Err::<String, _>("primary failed".into()), None).await },
    async move { ctx2.step(|_| Err::<String, _>("secondary failed".into()), None).await },
).await;

// Error contains information about all failures
assert!(result.is_err());

§When to Use

Use any! for fallback patterns where you want the first successful result and don’t care which source provides it. Unlike race!, any! ignores failures and only returns an error if ALL futures fail.

§See Also

  • all! - Wait for all to succeed
  • race! - Return first to settle (success or failure)
  • all_settled! - Collect all outcomes