Skip to content

23 · Concurrency

Use a fallback on deadline expiry

await fetch() within 2s else cached chooses cached only when the deadline expires. The fallback runs after cancellation and cleanup finish. A task's own Result error remains part of its return value.

On this page

await fetch() within 2s else cached chooses cached only when the deadline expires. The fallback runs after cancellation and cleanup finish. A task's own Result error remains part of its return value.

Complete example

Run this example locally with the installed CLI and host scheduler.

Use a fallback on deadline expiry.

03-deadline-fallback.zolo
// A cached answer is consulted only after the pending producer has drained.

let mut closed = 0
let mut cache_reads = 0
let mut started = false

async fn refresh() -> int {
    defer { closed += 1 }
    started = true
    sleep 10ms
    42
}

fn cached() -> int {
    assert(closed == 1)
    cache_reads += 1
    7
}

let pending = refresh()
// Let the producer enter and register its defer before imposing the deadline.

async fn scheduler_turn() {}
await scheduler_turn()
assert(started)
let value = await pending within 1ns else cached()
assert(value == 7)
assert(cache_reads == 1)

async fn ready() -> int = 11
let fresh = await ready() within 1s else cached()
assert(fresh == 11)
assert(cache_reads == 1)
print("deadline fallback: {value}, fresh: {fresh}")

Requires the Zolo CLI/host — open in the playground or run locally.

Reference

Reference · Source program

zolo run examples/features/75-syntax-four-async/03-deadline-fallback.zolo --no-cache

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

enespt-br