Skip to content

Std.Task

effect TaskGroup[E, R]

The capability to start tasks whose results are awaited. E and R are the fails and uses of the tasks. Only Task.Group provides it ([task.group]).

  • fn start[A](f: fn() -> A fails E uses { TaskGroup[E, R], R }) -> Task[A]

    Starts f as a task of the group. The task starts running the next time the starting task suspends. f may use the group itself, to start siblings.

type Task[A]

A task started in a group. It lives only while the capability of the group does ([task.group]).

handler Group[E, R]: TaskGroup[E, R]

Provides TaskGroup. When the body ends, its release interrupts the tasks left and waits until they have unwound. A failure of a task interrupts its siblings and fails the with.

effect Spawn[E, R]

The capability to start tasks whose results are not awaited. Only Task.Background provides it ([task.background]).

  • fn start(f: fn() -> () fails E uses { Spawn[E, R], R }) -> ()

    Starts f as a task. The task starts running the next time the starting task suspends.

handler Background[E, R]: Spawn[E, R]
init(on_failure: fn(Cause[E]) -> () uses { R })

Provides Spawn. Its release waits until the tasks have ended, without interrupting them. A failure of a task goes to on_failure, called in that task after it has unwound.

fn join[A](task: Task[A]) -> A uses { Async, task }

Waits for the task to end and returns its value.

fn all[A, E, R](fs: List[fn() -> A fails E uses { R }]) -> List[A] fails E uses { Async, R }

Runs all of fs concurrently and returns their values in order. If one fails, the others are interrupted and all fails.

fn race[A, E, R](fs: List[fn() -> A fails E uses { R }]) -> A fails E uses { Async, R }

Runs all of fs concurrently and takes the first to end; the others are interrupted. If the first to end fails, race fails with that error.

fn both[A, B, E, R](f: fn() -> A fails E uses { R }, g: fn() -> B fails E uses { R }) -> (A, B) fails E uses { Async, R }

Runs f and g concurrently and returns both values. If one fails, the other is interrupted and both fails.

fn map[A, B, E, R](xs: List[A], f: fn(A) -> B fails E uses { R }) -> List[B] fails E uses { Async, R }

Applies f to each element of xs concurrently and returns the results in order. If one fails, the others are interrupted and map fails.

fn timeout[A, E, R](ms: Int, f: fn() -> A fails E uses { R }) -> Option[A] fails E uses { Async, Clock, R }

Waits for f up to ms milliseconds. Returns Some with its value if it ends in time; otherwise interrupts it and returns None. Wrap it in Option.or_fail to fail instead. The time is measured with Clock.