Header menu logo TDesu.FSharp

Task Module

Task combinators — map, bind, zip, catch, loop, and more for Task{T}.

All functions are inline for zero-overhead abstractions. Thread-safe by design.

Functions and values

Function or value Description

asUnit t

Full Usage: asUnit t

Parameters:
Returns: Task<unit>
Modifiers: inline

Converts a non-generic Task to Task<unit>. The non-generic task to convert.

t : Task
Returns: Task<unit>

bind f t

Full Usage: bind f t

Parameters:
Returns: Task<'TResult>
Modifiers: inline
Type parameters: 'T, 'TResult

Chains a task-returning function on the result of a task. The binding function returning a new task. The input task.

f : 'T -> Task<'TResult>
t : Task<'T>
Returns: Task<'TResult>

catch t

Full Usage: catch t

Parameters:
    t : Task<'T> - The task to execute inside a try/catch.

Returns: Task<Result<'T, exn>>
Modifiers: inline
Type parameters: 'T

Runs a task inside a try/catch, returning Ok on success or Error(exn) on failure.

t : Task<'T>

The task to execute inside a try/catch.

Returns: Task<Result<'T, exn>>

fireAndForget onError f

Full Usage: fireAndForget onError f

Parameters:
    onError : exn -> unit - Callback invoked with the exception if the task faults.
    f : unit -> Task - Factory that produces the task to run.

Modifiers: inline

Fire-and-forget: starts a task without awaiting. Logs exceptions via onError.

onError should not throw; if it does, the exception is silently swallowed.

onError : exn -> unit

Callback invoked with the exception if the task faults.

f : unit -> Task

Factory that produces the task to run.

getResult t

Full Usage: getResult t

Parameters:
Returns: 'a
Modifiers: inline
Type parameters: 'a

Synchronously gets the result of a Task (ConfigureAwait false). The task whose result to retrieve.

t : Task<'a>
Returns: 'a

ignore t

Full Usage: ignore t

Parameters:
Returns: Task<unit>
Modifiers: inline
Type parameters: 'a

Discards the task result, returning Task<unit>. The task whose result to discard.

t : Task<'a>
Returns: Task<unit>

loop step state

Full Usage: loop step state

Parameters:
    step : 'State -> Task<Loop<'State, 'Result>> - One iteration: takes the current state and decides whether to continue with a new one or stop with the result.
    state : 'State - The state handed to the first step.

Returns: Task<'Result>

Runs step again and again, threading a state through, until it answers Stop. This is how to write a recursive task { } without recursion: a loop whose body is an ordinary task-returning function.

return! step next inside a task { } is not a tail call. The builder awaits the inner task, so every activation stays registered as the continuation of the one after it: a loop written that way keeps one state machine per iteration alive until it ends (measured on a socket read loop: 568 bytes per iteration, released only when the connection dropped) and, when the awaited operations complete synchronously, nests real stack frames — a 1 MB thread-pool stack overflowed between 1,000 and 1,500 iterations.

Here each step call is its own task, awaited from one while, so the loop runs in constant heap and constant stack however long it lives. Exceptions from a step end the loop and fault the returned task; cancellation is the step's business — make it answer Stop or throw.

step : 'State -> Task<Loop<'State, 'Result>>

One iteration: takes the current state and decides whether to continue with a new one or stop with the result.

state : 'State

The state handed to the first step.

Returns: Task<'Result>
Example

 let receiveLoop (transport: ITransport) =
     Task.loop (fun () -> task {
         match! transport.ReceiveAsync ct with
         | Ok frame -> handle frame; return Loop.Continue ()
         | Error e -> return Loop.Stop e
     }) ()
val receiveLoop: transport: 'a -> 'b
val transport: 'a
val task: TaskBuilder
union case Result.Ok: ResultValue: 'T -> Result<'T,'TError>
union case Result.Error: ErrorValue: 'TError -> Result<'T,'TError>

map f t

Full Usage: map f t

Parameters:
    f : 'T -> 'TResult
    t : Task<'T>

Returns: Task<'TResult>
Modifiers: inline
Type parameters: 'T, 'TResult

Maps the result of a task with the given function. The mapping function. The input task.

f : 'T -> 'TResult
t : Task<'T>
Returns: Task<'TResult>

map2 f t1 t2

Full Usage: map2 f t1 t2

Parameters:
    f : 'T1 -> 'T2 -> 'TResult
    t1 : Task<'T1>
    t2 : Task<'T2>

Returns: Task<'TResult>
Modifiers: inline
Type parameters: 'T1, 'T2, 'TResult

Maps a function over two task results, running them concurrently. The mapping function taking two values. The first task. The second task.

f : 'T1 -> 'T2 -> 'TResult
t1 : Task<'T1>
t2 : Task<'T2>
Returns: Task<'TResult>

parallelThrottle maxConcurrent items f

Full Usage: parallelThrottle maxConcurrent items f

Parameters:
    maxConcurrent : int - Maximum number of tasks allowed to run concurrently.
    items : 'T seq - The input sequence of items to process. Null is treated as empty.
    f : 'T -> Task<'TResult> - Async function applied to each item.

Returns: Task<'TResult[]>

Runs tasks in parallel with a throttle (max concurrent). Returns all results.

A null items sequence is treated as empty (returns an empty array). The semaphore is guaranteed to outlive every task that can still call Release() on it: if enumerating items or awaiting the throttle throws (e.g. a cancelled wait or a throwing enumerator), every already-spawned task is drained before the semaphore is disposed, so none of them can release an already-disposed SemaphoreSlim.

maxConcurrent : int

Maximum number of tasks allowed to run concurrently.

items : 'T seq

The input sequence of items to process. Null is treated as empty.

f : 'T -> Task<'TResult>

Async function applied to each item.

Returns: Task<'TResult[]>

parallelThrottleUnit maxConcurrent items f

Full Usage: parallelThrottleUnit maxConcurrent items f

Parameters:
    maxConcurrent : int - Maximum number of tasks allowed to run concurrently.
    items : 'T seq - The input sequence of items to process. Null is treated as empty.
    f : 'T -> Task<unit> - Async function applied to each item.

Returns: Task<unit>

Runs tasks in parallel with a throttle, ignoring results.

Same null-input and semaphore-lifetime guarantees as parallelThrottle: a null items sequence is treated as empty, and the semaphore is disposed only after every spawned task has been drained, even if the loop itself throws.

maxConcurrent : int

Maximum number of tasks allowed to run concurrently.

items : 'T seq

The input sequence of items to process. Null is treated as empty.

f : 'T -> Task<unit>

Async function applied to each item.

Returns: Task<unit>

runSynchronously t

Full Usage: runSynchronously t

Parameters:
    t : 'a

Blocks until the task completes (ConfigureAwait false). No-op if null or already completed. The task to wait on.

t : 'a

singleton v

Full Usage: singleton v

Parameters:
    v : 'T

Returns: Task<'T>
Modifiers: inline
Type parameters: 'T

Wraps a value in a completed task. The value to wrap.

v : 'T
Returns: Task<'T>

waitUntil timeout condition

Full Usage: waitUntil timeout condition

Parameters:
    timeout : TimeSpan - Maximum duration to wait.
    condition : unit -> bool - Predicate polled until it returns true.

Returns: Task<bool>

Polls a condition with short delays until it returns true or timeout is reached. Returns true if condition was met, false on timeout.

timeout : TimeSpan

Maximum duration to wait.

condition : unit -> bool

Predicate polled until it returns true.

Returns: Task<bool>

zip t1 t2

Full Usage: zip t1 t2

Parameters:
Returns: Task<'T1 * 'T2>
Modifiers: inline
Type parameters: 'T1, 'T2

Combines two tasks into a tuple, running them concurrently. The first task. The second task.

t1 : Task<'T1>
t2 : Task<'T2>
Returns: Task<'T1 * 'T2>

zip3 t1 t2 t3

Full Usage: zip3 t1 t2 t3

Parameters:
Returns: Task<'T1 * 'T2 * 'T3>
Modifiers: inline
Type parameters: 'T1, 'T2, 'T3

Combines three tasks into a triple, running them concurrently. The first task. The second task. The third task.

t1 : Task<'T1>
t2 : Task<'T2>
t3 : Task<'T3>
Returns: Task<'T1 * 'T2 * 'T3>

Type something to start searching.