Logo FSharp.Core

Task Module

Contains camelCase module-level functions for Task computations.

Functions and values

Function or value Description

Task.bind binder task

Full Usage: Task.bind binder task

Parameters:
    binder : 'T -> Task<'U> - A function that takes the result of the task and returns a new task.
    task : Task<'T> - The input task.

Returns: Task<'U> A task that performs a monadic bind on the result of task.
Modifiers: inline
Type parameters: 'T, 'U

Creates a task that passes the result of the given task to the binder function.

binder : 'T -> Task<'U>

A function that takes the result of the task and returns a new task.

task : Task<'T>

The input task.

Returns: Task<'U>

A task that performs a monadic bind on the result of task.

Example

 let t = Task.result 21 |> Task.bind (fun x -> Task.result (x * 2))
 t.Result // evaluates to 42
val t: obj

Task.catch task

Full Usage: Task.catch task

Parameters:
    task : Task<'T> - The input Task.

Returns: Task<Result<'T, exn>> A Task that yields a Result: Ok with the outcome on success, or Error with the exception on failure. Propagates the underlying cancellation exception when task is canceled.
Type parameters: 'T

Creates a Task that reifies the outcome of the given Task as a Result: Ok on success, Error on failure, so faults become values. Cancellation still propagates.

OperationCanceledException and derived types such as TaskCanceledException propagate unchanged (and the task remains Canceled) in order to maintain cancellation semantics.

task : Task<'T>

The input Task.

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

A Task that yields a Result: Ok with the outcome on success, or Error with the exception on failure. Propagates the underlying cancellation exception when task is canceled.

Example

 let safeDiv x y = task { return x / y } |> Task.catch
 (safeDiv 10 2).Result // evaluates to Ok 5
 (safeDiv 10 0).Result // evaluates to Error (DivideByZeroException ...)
val safeDiv: x: int -> y: int -> 'a
val x: int
val y: int
val task: TaskBuilder
Multiple items
module Result from Microsoft.FSharp.Core

--------------------
type Result<'T,'TError> = | Ok of ResultValue: 'T | Error of ErrorValue: 'TError

Task.catchWith handler task

Full Usage: Task.catchWith handler task

Parameters:
    handler : exn -> 'T - A function to handle (non-cancellation) exceptions, yielding a recovery value based on the exception. Any exception thrown by handler will propagate.
    task : Task<'T> - The input Task.

Returns: Task<'T> A Task that yields the result of task on success, or handler exn on failure. Propagates the underlying cancellation exception when task is canceled.
Modifiers: inline
Type parameters: 'T

Creates a Task that yields the original result on success, or the result of handler exn for non-cancellation exceptions.

OperationCanceledException and derived types such as TaskCanceledException propagate unchanged (and the task remains Canceled) in order to maintain cancellation semantics, and therefore are never passed to handler.

handler : exn -> 'T

A function to handle (non-cancellation) exceptions, yielding a recovery value based on the exception. Any exception thrown by handler will propagate.

task : Task<'T>

The input Task.

Returns: Task<'T>

A Task that yields the result of task on success, or handler exn on failure. Propagates the underlying cancellation exception when task is canceled.

Example

 let safeDiv x y =
     task { return x / y }
     |> Task.catchWith (fun _ -> 0)
 (safeDiv 10 0).Result // evaluates to 0
val safeDiv: x: int -> y: int -> 'a
val x: int
val y: int
val task: TaskBuilder
Multiple items
module Result from Microsoft.FSharp.Core

--------------------
type Result<'T,'TError> = | Ok of ResultValue: 'T | Error of ErrorValue: 'TError

Task.empty

Full Usage: Task.empty

Returns: Task<unit>

A completed task that returns unit. This is a Task<unit> (not the non-generic Task.CompletedTask).

Returns: Task<unit>
Example

 Task.empty.Result // evaluates to ()

Task.ignore task

Full Usage: Task.ignore task

Parameters:
    task : Task<'T> - The input task.

Returns: Task<unit> A task that is equivalent to the input task, but disregards the result.
Modifiers: inline
Type parameters: 'T

Creates a task that runs the given task and ignores its result.

task : Task<'T>

The input task.

Returns: Task<unit>

A task that is equivalent to the input task, but disregards the result.

Example

 let t : Task<unit> = Task.result 42 |> Task.ignore<int>
 t.Result // evaluates to ()
val t: obj
type unit = Unit
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int

Task.map mapping task

Full Usage: Task.map mapping task

Parameters:
    mapping : 'T -> 'U - The function to apply to the result.
    task : Task<'T> - The input task.

Returns: Task<'U> A task that applies mapping to the result of task.
Modifiers: inline
Type parameters: 'T, 'U

Creates a task that applies the mapping function to the result of the given task.

mapping : 'T -> 'U

The function to apply to the result.

task : Task<'T>

The input task.

Returns: Task<'U>

A task that applies mapping to the result of task.

Example

 let t = Task.result 21 |> Task.map (fun x -> x * 2)
 t.Result // evaluates to 42
val t: obj

Task.ofValueTask valueTask

Full Usage: Task.ofValueTask valueTask

Parameters:
    valueTask : ValueTask<'T> - The input value task.

Returns: Task<'T> A task equivalent to the given value task.
Modifiers: inline
Type parameters: 'T

Converts a ValueTask to a Task.

valueTask : ValueTask<'T>

The input value task.

Returns: Task<'T>

A task equivalent to the given value task.

Example

 let vt = ValueTask<int>(42)
 let t = Task.ofValueTask vt
 t.Result // evaluates to 42
val vt: obj
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
val t: obj

Task.parallelDoLimit maxDegreeOfParallelism ct computations

Full Usage: Task.parallelDoLimit maxDegreeOfParallelism ct computations

Parameters:
    maxDegreeOfParallelism : int - The maximum number of tasks to run concurrently. Must be > 0.
    ct : CancellationToken - An outer cancellation token used to cancel the parallel request. When multiple tasks can run concurrently, task factories receive a linked token that is also canceled if a sibling faults; otherwise they receive ct directly.
    computations : (CancellationToken -> Task<unit>) seq - A sequence of unit task start functions accepting a CancellationToken.

Returns: Task<unit> A task that runs all inputs with the specified parallelism limit and returns unit.

Creates a task that executes the computations in parallel, with concurrency limited to at most maxDegreeOfParallelism.

The relative start and completion order per computation is arbitrary.

If any of the computations Fault, the governing CancellationToken of its siblings will be Canceled.

Where multiple computations Fault, a single exception is propagated.

maxDegreeOfParallelism : int

The maximum number of tasks to run concurrently. Must be > 0.

ct : CancellationToken

An outer cancellation token used to cancel the parallel request. When multiple tasks can run concurrently, task factories receive a linked token that is also canceled if a sibling faults; otherwise they receive ct directly.

computations : (CancellationToken -> Task<unit>) seq

A sequence of unit task start functions accepting a CancellationToken.

Returns: Task<unit>

A task that runs all inputs with the specified parallelism limit and returns unit.

Example

 task {
     return!
         seq { for i in 1..10 -> fun _ct -> task { printfn "%d" i } } // NOTE output order can vary
         |> Task.parallelDoLimit 3 CancellationToken.None
 }
val task: TaskBuilder
Multiple items
val seq: sequence: 'T seq -> 'T seq

--------------------
type 'T seq = System.Collections.Generic.IEnumerable<'T>
val i: int
val _ct: obj
val printfn: format: Printf.TextWriterFormat<'T> -> 'T

Task.parallelLimit maxDegreeOfParallelism ct computations

Full Usage: Task.parallelLimit maxDegreeOfParallelism ct computations

Parameters:
    maxDegreeOfParallelism : int - The maximum number of tasks to run concurrently. Must be > 0.
    ct : CancellationToken - An outer cancellation token used to cancel the parallel request. When multiple tasks can run concurrently, task factories receive a linked token that is also canceled if a sibling faults; otherwise they receive ct directly.
    computations : (CancellationToken -> Task<'T>) seq - A sequence of task start functions accepting a CancellationToken.

Returns: Task<'T[]> A task yielding an array of the results of computations in the order they were supplied.
Type parameters: 'T

Creates a task that executes each of the computations in parallel with concurrency limited to at most maxDegreeOfParallelism, returning an array of their results in order of the input sequence.

The relative start and completion order per computation is arbitrary.

If any of the computations Fault, the governing CancellationToken of its siblings will be Canceled.

Where multiple computations Fault, a single exception is propagated.

maxDegreeOfParallelism : int

The maximum number of tasks to run concurrently. Must be > 0.

ct : CancellationToken

An outer cancellation token used to cancel the parallel request. When multiple tasks can run concurrently, task factories receive a linked token that is also canceled if a sibling faults; otherwise they receive ct directly.

computations : (CancellationToken -> Task<'T>) seq

A sequence of task start functions accepting a CancellationToken.

Returns: Task<'T[]>

A task yielding an array of the results of computations in the order they were supplied.

Example

 task {
     return!
         seq { for i in 1..10 -> fun _ct -> Task.result (i * i) }
         |> Task.parallelLimit 3 CancellationToken.None
 } // returns [| 1; 4; 9; 16; 25; 36; 49; 64; 81; 100 |]
val task: TaskBuilder
Multiple items
val seq: sequence: 'T seq -> 'T seq

--------------------
type 'T seq = System.Collections.Generic.IEnumerable<'T>
val i: int
val _ct: obj

Task.result value

Full Usage: Task.result value

Parameters:
    value : 'T - The value to return.

Returns: Task<'T> A completed task that returns value.
Modifiers: inline
Type parameters: 'T

Creates a task that returns the given value.

value : 'T

The value to return.

Returns: Task<'T>

A completed task that returns value.

Example

 let t = Task.result 42
 t.Result // evaluates to 42
val t: obj

Task.sequential ct computations

Full Usage: Task.sequential ct computations

Parameters:
Returns: Task<'T[]> A task yielding an array of the results of computations in the order they were supplied.
Type parameters: 'T

Creates a task that executes each of the computations in sequence, returning an array of their results in order of the input sequence.

ct : CancellationToken

A cancellation token to pass to each task factory.

computations : (CancellationToken -> Task<'T>) seq

A sequence of task start functions accepting a CancellationToken.

Returns: Task<'T[]>

A task yielding an array of the results of computations in the order they were supplied.

Example

 task {
     return!
         seq { for i in 1..10 -> fun _ct -> Task.result (i * i) }
         |> Task.sequential CancellationToken.None
 } // returns [| 1; 4; 9; 16; 25; 36; 49; 64; 81; 100 |]
val task: TaskBuilder
Multiple items
val seq: sequence: 'T seq -> 'T seq

--------------------
type 'T seq = System.Collections.Generic.IEnumerable<'T>
val i: int
val _ct: obj

Task.sequentialDo ct computations

Full Usage: Task.sequentialDo ct computations

Parameters:
Returns: Task<unit> A task that runs all inputs in sequence and returns unit.

Creates a task that executes each of the computations in sequence, returning unit.

ct : CancellationToken

A cancellation token to pass to each task factory.

computations : (CancellationToken -> Task<unit>) seq

A sequence of unit task start functions accepting a CancellationToken.

Returns: Task<unit>

A task that runs all inputs in sequence and returns unit.

Example

 task {
     return!
         seq { for i in 1..10 -> fun _ct -> task { printfn "%d" i } }
         // NOTE numbers are guaranteed to be printed in order 1..10
         |> Task.sequentialDo CancellationToken.None
 }
val task: TaskBuilder
Multiple items
val seq: sequence: 'T seq -> 'T seq

--------------------
type 'T seq = System.Collections.Generic.IEnumerable<'T>
val i: int
val _ct: obj
val printfn: format: Printf.TextWriterFormat<'T> -> 'T

Task.startAsyncImmediate ct computation

Full Usage: Task.startAsyncImmediate ct computation

Parameters:
    ct : CancellationToken - A cancellation token to use for computation.
    computation : Async<'T> - The async computation to start.

Returns: Task<'T> A task representing the result of computation.
Type parameters: 'T

Starts the computation on the current thread, returning a Task that represents its result.

The computation begins executing synchronously on the calling thread, offloading only at the point where it first suspends (mirroring Async.StartImmediateAsTask). ct flows into the computation, so cancelling it cancels computation, and the resulting task observes that cancellation.

ct : CancellationToken

A cancellation token to use for computation.

computation : Async<'T>

The async computation to start.

Returns: Task<'T>

A task representing the result of computation.

Example

 use cts = new CancellationTokenSource()
 task {
     return!
         async { return 42 }
         |> Task.startAsyncImmediate cts.Token
 } // returns 42
val cts: obj
val task: TaskBuilder
val async: AsyncBuilder

Type something to start searching.