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.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

Type something to start searching.