Logo FSharp.Core

Async Module

Contains camelCase module-level functions for Async computations.

Functions and values

Function or value Description

bind binder computation

Full Usage: bind binder computation

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

Returns: Async<'U> An asynchronous computation that performs a monadic bind on the result of computation.
Modifiers: inline
Type parameters: 'T, 'U

Creates an asynchronous computation that passes the result of the given computation to the binder function.

binder : 'T -> Async<'U>

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

computation : Async<'T>

The input computation.

Returns: Async<'U>

An asynchronous computation that performs a monadic bind on the result of computation.

Example

 let computation = Async.result 21 |> Async.bind (fun x -> Async.result (x * 2))
 computation |> Async.RunSynchronouslyImmediate // evaluates to 42
val computation: obj
Multiple items
type Async = static member AsBeginEnd: computation: ('Arg -> Async<'T>) -> ('Arg * AsyncCallback * objnull -> IAsyncResult) * (IAsyncResult -> 'T) * (IAsyncResult -> unit) static member AwaitEvent: event: IEvent<'Del,'T> * ?cancelAction: (unit -> unit) -> Async<'T> (requires delegate and 'Del :> Delegate and 'Del: not null) static member AwaitIAsyncResult: iar: IAsyncResult * ?millisecondsTimeout: int -> Async<bool> static member AwaitTask: task: Task<'T> -> Async<'T> + 1 overload static member AwaitWaitHandle: waitHandle: WaitHandle * ?millisecondsTimeout: int -> Async<bool> static member CancelDefaultToken: unit -> unit static member Catch: computation: Async<'T> -> Async<Choice<'T,exn>> static member Choice: computations: Async<'T option> seq -> Async<'T option> static member FromBeginEnd: beginAction: (AsyncCallback * objnull -> IAsyncResult) * endAction: (IAsyncResult -> 'T) * ?cancelAction: (unit -> unit) -> Async<'T> + 3 overloads static member FromContinuations: callback: (('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) -> unit) -> Async<'T> ...

--------------------
type Async<'T>

catch computation

Full Usage: catch computation

Parameters:
    computation : Async<'T> - The input computation.

Returns: Async<Result<'T, exn>> An asynchronous computation that yields a Result: Ok with the outcome on success, or Error with the exception on failure. Propagates the underlying cancellation exception when cancellation occurs.
Type parameters: 'T

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

OperationCanceledException and derived types such as TaskCanceledException propagate unchanged.

computation : Async<'T>

The input computation.

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

An asynchronous computation that yields a Result: Ok with the outcome on success, or Error with the exception on failure. Propagates the underlying cancellation exception when cancellation occurs.

Example

 let safeDiv x y =
     async { return x / y } |> Async.catch
 safeDiv 10 2 |> Async.RunSynchronouslyImmediate // evaluates to Ok 5
 safeDiv 10 0 |> Async.RunSynchronouslyImmediate // evaluates to Error (DivideByZeroException ...)
val safeDiv: x: int -> y: int -> 'a
val x: int
val y: int
val async: AsyncBuilder
Multiple items
type Async = static member AsBeginEnd: computation: ('Arg -> Async<'T>) -> ('Arg * AsyncCallback * objnull -> IAsyncResult) * (IAsyncResult -> 'T) * (IAsyncResult -> unit) static member AwaitEvent: event: IEvent<'Del,'T> * ?cancelAction: (unit -> unit) -> Async<'T> (requires delegate and 'Del :> Delegate and 'Del: not null) static member AwaitIAsyncResult: iar: IAsyncResult * ?millisecondsTimeout: int -> Async<bool> static member AwaitTask: task: Task<'T> -> Async<'T> + 1 overload static member AwaitWaitHandle: waitHandle: WaitHandle * ?millisecondsTimeout: int -> Async<bool> static member CancelDefaultToken: unit -> unit static member Catch: computation: Async<'T> -> Async<Choice<'T,exn>> static member Choice: computations: Async<'T option> seq -> Async<'T option> static member FromBeginEnd: beginAction: (AsyncCallback * objnull -> IAsyncResult) * endAction: (IAsyncResult -> 'T) * ?cancelAction: (unit -> unit) -> Async<'T> + 3 overloads static member FromContinuations: callback: (('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) -> unit) -> Async<'T> ...

--------------------
type Async<'T>

catchWith handler computation

Full Usage: catchWith handler computation

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.
    computation : Async<'T> - The input computation.

Returns: Async<'T> An asynchronous computation that yields the result of computation on success, or handler exn on failure. Propagates the underlying cancellation exception where cancellation occurs.
Type parameters: 'T

Creates an asynchronous computation 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 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.

computation : Async<'T>

The input computation.

Returns: Async<'T>

An asynchronous computation that yields the result of computation on success, or handler exn on failure. Propagates the underlying cancellation exception where cancellation occurs.

Example

 let safeDiv x y =
     async { return x / y }
     |> Async.catchWith (fun _ -> 0)
 safeDiv 10 0 |> Async.RunSynchronouslyImmediate // evaluates to 0
val safeDiv: x: int -> y: int -> 'a
val x: int
val y: int
val async: AsyncBuilder
Multiple items
type Async = static member AsBeginEnd: computation: ('Arg -> Async<'T>) -> ('Arg * AsyncCallback * objnull -> IAsyncResult) * (IAsyncResult -> 'T) * (IAsyncResult -> unit) static member AwaitEvent: event: IEvent<'Del,'T> * ?cancelAction: (unit -> unit) -> Async<'T> (requires delegate and 'Del :> Delegate and 'Del: not null) static member AwaitIAsyncResult: iar: IAsyncResult * ?millisecondsTimeout: int -> Async<bool> static member AwaitTask: task: Task<'T> -> Async<'T> + 1 overload static member AwaitWaitHandle: waitHandle: WaitHandle * ?millisecondsTimeout: int -> Async<bool> static member CancelDefaultToken: unit -> unit static member Catch: computation: Async<'T> -> Async<Choice<'T,exn>> static member Choice: computations: Async<'T option> seq -> Async<'T option> static member FromBeginEnd: beginAction: (AsyncCallback * objnull -> IAsyncResult) * endAction: (IAsyncResult -> 'T) * ?cancelAction: (unit -> unit) -> Async<'T> + 3 overloads static member FromContinuations: callback: (('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) -> unit) -> Async<'T> ...

--------------------
type Async<'T>

empty

Full Usage: empty

Returns: Async<unit>

An asynchronous computation that returns unit. This is equivalent to async.Zero().

Returns: Async<unit>
Example

 Async.empty |> Async.RunSynchronouslyImmediate // evaluates to ()
Multiple items
type Async = static member AsBeginEnd: computation: ('Arg -> Async<'T>) -> ('Arg * AsyncCallback * objnull -> IAsyncResult) * (IAsyncResult -> 'T) * (IAsyncResult -> unit) static member AwaitEvent: event: IEvent<'Del,'T> * ?cancelAction: (unit -> unit) -> Async<'T> (requires delegate and 'Del :> Delegate and 'Del: not null) static member AwaitIAsyncResult: iar: IAsyncResult * ?millisecondsTimeout: int -> Async<bool> static member AwaitTask: task: Task<'T> -> Async<'T> + 1 overload static member AwaitWaitHandle: waitHandle: WaitHandle * ?millisecondsTimeout: int -> Async<bool> static member CancelDefaultToken: unit -> unit static member Catch: computation: Async<'T> -> Async<Choice<'T,exn>> static member Choice: computations: Async<'T option> seq -> Async<'T option> static member FromBeginEnd: beginAction: (AsyncCallback * objnull -> IAsyncResult) * endAction: (IAsyncResult -> 'T) * ?cancelAction: (unit -> unit) -> Async<'T> + 3 overloads static member FromContinuations: callback: (('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) -> unit) -> Async<'T> ...

--------------------
type Async<'T>

ignore computation

Full Usage: ignore computation

Parameters:
    computation : Async<'T> - The input computation.

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

Creates an asynchronous computation that runs the given computation and ignores its result.

computation : Async<'T>

The input computation.

Returns: Async<unit>

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

Example

 let readFile filename numBytes: Async<unit> =
     async {
         use file = System.IO.File.OpenRead(filename)
         do! file.AsyncRead(numBytes) |> Async.ignore<byte[]>
     }
val readFile: filename: string -> numBytes: int -> Async<unit>
val filename: string
val numBytes: int
Multiple items
type Async = static member AsBeginEnd: computation: ('Arg -> Async<'T>) -> ('Arg * AsyncCallback * objnull -> IAsyncResult) * (IAsyncResult -> 'T) * (IAsyncResult -> unit) static member AwaitEvent: event: IEvent<'Del,'T> * ?cancelAction: (unit -> unit) -> Async<'T> (requires delegate and 'Del :> Delegate and 'Del: not null) static member AwaitIAsyncResult: iar: IAsyncResult * ?millisecondsTimeout: int -> Async<bool> static member AwaitTask: task: Task<'T> -> Async<'T> + 1 overload static member AwaitWaitHandle: waitHandle: WaitHandle * ?millisecondsTimeout: int -> Async<bool> static member CancelDefaultToken: unit -> unit static member Catch: computation: Async<'T> -> Async<Choice<'T,exn>> static member Choice: computations: Async<'T option> seq -> Async<'T option> static member FromBeginEnd: beginAction: (AsyncCallback * objnull -> IAsyncResult) * endAction: (IAsyncResult -> 'T) * ?cancelAction: (unit -> unit) -> Async<'T> + 3 overloads static member FromContinuations: callback: (('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) -> unit) -> Async<'T> ...

--------------------
type Async<'T>
type unit = Unit
val async: AsyncBuilder
val file: System.IO.FileStream
namespace System
namespace System.IO
type File = static member AppendAllBytes: path: string * bytes: byte array -> unit + 1 overload static member AppendAllBytesAsync: path: string * bytes: byte array * ?cancellationToken: CancellationToken -> Task + 1 overload static member AppendAllLines: path: string * contents: string seq -> unit + 1 overload static member AppendAllLinesAsync: path: string * contents: string seq * encoding: Encoding * ?cancellationToken: CancellationToken -> Task + 1 overload static member AppendAllText: path: string * contents: ReadOnlySpan<char> -> unit + 3 overloads static member AppendAllTextAsync: path: string * contents: ReadOnlyMemory<char> * encoding: Encoding * ?cancellationToken: CancellationToken -> Task + 3 overloads static member AppendText: path: string -> StreamWriter static member Copy: sourceFileName: string * destFileName: string -> unit + 1 overload static member Create: path: string -> FileStream + 2 overloads static member CreateHardLink: path: string * pathToTarget: string -> FileSystemInfo ...
<summary>Provides static methods for the creation, copying, deletion, moving, and opening of a single file, and aids in the creation of <see cref="T:System.IO.FileStream" /> objects.</summary>
System.IO.File.OpenRead(path: string) : System.IO.FileStream
member System.IO.Stream.AsyncRead: count: int -> Async<byte array>
member System.IO.Stream.AsyncRead: buffer: byte array * ?offset: int * ?count: int -> Async<int>
Multiple items
val byte: value: 'T -> byte (requires member op_Explicit)

--------------------
type byte = System.Byte

--------------------
type byte<'Measure> = byte

Example

 let computation : Async<unit> = Async.result 42 |> Async.ignore<int>
 computation |> Async.RunSynchronously // evaluates to ()
val computation: Async<unit>
Multiple items
type Async = static member AsBeginEnd: computation: ('Arg -> Async<'T>) -> ('Arg * AsyncCallback * objnull -> IAsyncResult) * (IAsyncResult -> 'T) * (IAsyncResult -> unit) static member AwaitEvent: event: IEvent<'Del,'T> * ?cancelAction: (unit -> unit) -> Async<'T> (requires delegate and 'Del :> Delegate and 'Del: not null) static member AwaitIAsyncResult: iar: IAsyncResult * ?millisecondsTimeout: int -> Async<bool> static member AwaitTask: task: Task<'T> -> Async<'T> + 1 overload static member AwaitWaitHandle: waitHandle: WaitHandle * ?millisecondsTimeout: int -> Async<bool> static member CancelDefaultToken: unit -> unit static member Catch: computation: Async<'T> -> Async<Choice<'T,exn>> static member Choice: computations: Async<'T option> seq -> Async<'T option> static member FromBeginEnd: beginAction: (AsyncCallback * objnull -> IAsyncResult) * endAction: (IAsyncResult -> 'T) * ?cancelAction: (unit -> unit) -> Async<'T> + 3 overloads static member FromContinuations: callback: (('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) -> unit) -> Async<'T> ...

--------------------
type Async<'T>
type unit = Unit
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

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

--------------------
type int<'Measure> = int
static member Async.RunSynchronously: computation: Async<'T> * ?timeout: int * ?cancellationToken: System.Threading.CancellationToken -> 'T

map mapping computation

Full Usage: map mapping computation

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

Returns: Async<'U> An asynchronous computation that applies mapping to the result of computation.
Modifiers: inline
Type parameters: 'T, 'U

Creates an asynchronous computation that applies the mapping function to the result of the given computation.

mapping : 'T -> 'U

The function to apply to the result.

computation : Async<'T>

The input computation.

Returns: Async<'U>

An asynchronous computation that applies mapping to the result of computation.

Example

 let computation = Async.result 21 |> Async.map (fun x -> x * 2)
 computation |> Async.RunSynchronouslyImmediate // evaluates to 42
val computation: obj
Multiple items
type Async = static member AsBeginEnd: computation: ('Arg -> Async<'T>) -> ('Arg * AsyncCallback * objnull -> IAsyncResult) * (IAsyncResult -> 'T) * (IAsyncResult -> unit) static member AwaitEvent: event: IEvent<'Del,'T> * ?cancelAction: (unit -> unit) -> Async<'T> (requires delegate and 'Del :> Delegate and 'Del: not null) static member AwaitIAsyncResult: iar: IAsyncResult * ?millisecondsTimeout: int -> Async<bool> static member AwaitTask: task: Task<'T> -> Async<'T> + 1 overload static member AwaitWaitHandle: waitHandle: WaitHandle * ?millisecondsTimeout: int -> Async<bool> static member CancelDefaultToken: unit -> unit static member Catch: computation: Async<'T> -> Async<Choice<'T,exn>> static member Choice: computations: Async<'T option> seq -> Async<'T option> static member FromBeginEnd: beginAction: (AsyncCallback * objnull -> IAsyncResult) * endAction: (IAsyncResult -> 'T) * ?cancelAction: (unit -> unit) -> Async<'T> + 3 overloads static member FromContinuations: callback: (('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) -> unit) -> Async<'T> ...

--------------------
type Async<'T>

result value

Full Usage: result value

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

Returns: Async<'T> An asynchronous computation that returns value when executed.
Modifiers: inline
Type parameters: 'T

Creates an asynchronous computation that returns the given value.

value : 'T

The value to return.

Returns: Async<'T>

An asynchronous computation that returns value when executed.

Example

 let computation = Async.result 42
 computation |> Async.RunSynchronouslyImmediate // evaluates to 42
val computation: obj
Multiple items
type Async = static member AsBeginEnd: computation: ('Arg -> Async<'T>) -> ('Arg * AsyncCallback * objnull -> IAsyncResult) * (IAsyncResult -> 'T) * (IAsyncResult -> unit) static member AwaitEvent: event: IEvent<'Del,'T> * ?cancelAction: (unit -> unit) -> Async<'T> (requires delegate and 'Del :> Delegate and 'Del: not null) static member AwaitIAsyncResult: iar: IAsyncResult * ?millisecondsTimeout: int -> Async<bool> static member AwaitTask: task: Task<'T> -> Async<'T> + 1 overload static member AwaitWaitHandle: waitHandle: WaitHandle * ?millisecondsTimeout: int -> Async<bool> static member CancelDefaultToken: unit -> unit static member Catch: computation: Async<'T> -> Async<Choice<'T,exn>> static member Choice: computations: Async<'T option> seq -> Async<'T option> static member FromBeginEnd: beginAction: (AsyncCallback * objnull -> IAsyncResult) * endAction: (IAsyncResult -> 'T) * ?cancelAction: (unit -> unit) -> Async<'T> + 3 overloads static member FromContinuations: callback: (('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) -> unit) -> Async<'T> ...

--------------------
type Async<'T>

Type something to start searching.