Logo FSharp.Core

AsyncTaskLikeExtensions Module

A module of extension members providing support for awaiting any task-like value via the GetAwaiter pattern.

Type extensions

Type extension Description

Async.Await(task)

Full Usage: Async.Await(task)

Parameters:
    task : ^TaskLike - The task-like value to await.

Returns: Async<'T>
Modifiers: inline
Type parameters: ^TaskLike, ^Awaiter, 'T (requires (member GetAwaiter : ^TaskLike -> ^Awaiter) and :> System.Runtime.CompilerServices.ICriticalNotifyCompletion and (member get_IsCompleted : ^Awaiter -> Microsoft.FSharp.Core.bool) and (member GetResult : ^Awaiter -> 'T))

Creates an asynchronous computation that will wait for the given task-like value to complete and return its result.

The value must satisfy the GetAwaiter pattern: it must have a GetAwaiter() method returning an awaiter implementing with IsCompleted and GetResult() members.

Exceptions thrown by GetResult() are propagated directly.

Unlike the and overloads, an carrying multiple inner exceptions is not preserved: the first inner exception surfaces (standard GetResult() semantics).

This overload uses statically resolved type parameters (SRTP) so it can accept any task-like type. The specific overloads for , , and are preferred when the argument type is known.

Extended Type: Async

task : ^TaskLike

The task-like value to await.

Returns: Async<'T>
Example

 // A minimal custom task-like type
 type MyTask<'T>(task: System.Threading.Tasks.Task<'T>) =
     member _.GetAwaiter() = task.GetAwaiter()

 let myTask = MyTask(System.Threading.Tasks.Task.FromResult 42)
 async {
     let! result = Async.Await myTask
     printfn $"Result: {result}"
 } |> Async.RunSynchronously
Multiple items
type MyTask<'T> = new: task: Task<'T> -> MyTask<'T> member GetAwaiter: unit -> TaskAwaiter<'T>

--------------------
new: task: System.Threading.Tasks.Task<'T> -> MyTask<'T>
'T
val task: System.Threading.Tasks.Task<'T>
namespace System
namespace System.Threading
namespace System.Threading.Tasks
Multiple items
type Task<'TResult> = inherit Task new: ``function`` : Func<obj,'TResult> * state: obj -> unit + 7 overloads member ConfigureAwait: continueOnCapturedContext: bool -> ConfiguredTaskAwaitable<'TResult> + 1 overload member ContinueWith: continuationAction: Action<Task<'TResult>,obj> * state: obj -> Task + 19 overloads member GetAwaiter: unit -> TaskAwaiter<'TResult> member WaitAsync: cancellationToken: CancellationToken -> Task<'TResult> + 4 overloads member Result: 'TResult static member Factory: TaskFactory<'TResult>
<summary>Represents an asynchronous operation that can return a value.</summary>
<typeparam name="TResult">The type of the result produced by this <see cref="T:System.Threading.Tasks.Task`1" />.</typeparam>


--------------------
type Task = interface IAsyncResult interface IDisposable new: action: Action -> unit + 7 overloads member ConfigureAwait: continueOnCapturedContext: bool -> ConfiguredTaskAwaitable + 1 overload member ContinueWith: continuationAction: Action<Task,obj> * state: obj -> Task + 19 overloads member Dispose: unit -> unit member GetAwaiter: unit -> TaskAwaiter member RunSynchronously: unit -> unit + 1 overload member Start: unit -> unit + 1 overload member Wait: unit -> unit + 5 overloads ...
<summary>Represents an asynchronous operation.</summary>

--------------------
System.Threading.Tasks.Task(``function`` : System.Func<'TResult>) : System.Threading.Tasks.Task<'TResult>
System.Threading.Tasks.Task(``function`` : System.Func<obj,'TResult>, state: obj) : System.Threading.Tasks.Task<'TResult>
System.Threading.Tasks.Task(``function`` : System.Func<'TResult>, cancellationToken: System.Threading.CancellationToken) : System.Threading.Tasks.Task<'TResult>
System.Threading.Tasks.Task(``function`` : System.Func<'TResult>, creationOptions: System.Threading.Tasks.TaskCreationOptions) : System.Threading.Tasks.Task<'TResult>
System.Threading.Tasks.Task(``function`` : System.Func<obj,'TResult>, state: obj, cancellationToken: System.Threading.CancellationToken) : System.Threading.Tasks.Task<'TResult>
System.Threading.Tasks.Task(``function`` : System.Func<obj,'TResult>, state: obj, creationOptions: System.Threading.Tasks.TaskCreationOptions) : System.Threading.Tasks.Task<'TResult>
System.Threading.Tasks.Task(``function`` : System.Func<'TResult>, cancellationToken: System.Threading.CancellationToken, creationOptions: System.Threading.Tasks.TaskCreationOptions) : System.Threading.Tasks.Task<'TResult>
System.Threading.Tasks.Task(``function`` : System.Func<obj,'TResult>, state: obj, cancellationToken: System.Threading.CancellationToken, creationOptions: System.Threading.Tasks.TaskCreationOptions) : System.Threading.Tasks.Task<'TResult>

--------------------
System.Threading.Tasks.Task(action: System.Action) : System.Threading.Tasks.Task
System.Threading.Tasks.Task(action: System.Action, cancellationToken: System.Threading.CancellationToken) : System.Threading.Tasks.Task
System.Threading.Tasks.Task(action: System.Action, creationOptions: System.Threading.Tasks.TaskCreationOptions) : System.Threading.Tasks.Task
System.Threading.Tasks.Task(action: System.Action<obj>, state: obj) : System.Threading.Tasks.Task
System.Threading.Tasks.Task(action: System.Action, cancellationToken: System.Threading.CancellationToken, creationOptions: System.Threading.Tasks.TaskCreationOptions) : System.Threading.Tasks.Task
System.Threading.Tasks.Task(action: System.Action<obj>, state: obj, cancellationToken: System.Threading.CancellationToken) : System.Threading.Tasks.Task
System.Threading.Tasks.Task(action: System.Action<obj>, state: obj, creationOptions: System.Threading.Tasks.TaskCreationOptions) : System.Threading.Tasks.Task
System.Threading.Tasks.Task(action: System.Action<obj>, state: obj, cancellationToken: System.Threading.CancellationToken, creationOptions: System.Threading.Tasks.TaskCreationOptions) : System.Threading.Tasks.Task
System.Threading.Tasks.Task.GetAwaiter() : System.Runtime.CompilerServices.TaskAwaiter<'T>
val myTask: MyTask<int>
System.Threading.Tasks.Task.FromResult<'TResult>(result: 'TResult) : System.Threading.Tasks.Task<'TResult>
val async: AsyncBuilder
val result: 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>
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
static member Async.RunSynchronously: computation: Async<'T> * ?timeout: int * ?cancellationToken: System.Threading.CancellationToken -> 'T
Prints Result: 42.

Type something to start searching.