Kanda Foundation 0.8.0
Loading...
Searching...
No Matches
AsyncUtils Class Reference

A collection of utilities and extensions to facilitate async programming. More...

Classes

struct  AsyncOperationAwaiter
 Custom await for a Unity AsyncOperation allowing it to be awaited in async/await context. More...
 
struct  UnityWebRequestAwaiter
 Custom await for UnityWebRequest allowing it to be awaited in async/await context. More...
 

Static Public Member Functions

static AsyncOperationAwaiter GetAwaiter (this AsyncOperation asyncOp)
 Extension method to easily get AsyncOperation awaiter in async/await context.
 
static UnityWebRequestAwaiter GetAwaiter (this UnityWebRequestAsyncOperation asyncOp)
 Extension method to easily get web request awaiter in async/await context.
 
static async Task YieldUntil (Func< bool > condition)
 Shorthand to wait for a condition to be met checking on each frame.
 
static async Task WaitUntil (Func< bool > condition, int pollingIntervalMs=100)
 Shorthand to wait for a condition to be met checking at a given polling interval.
 
static async Task< T > WithTimeout< T > (this Task< T > task, string taskName, int timeoutMs=10000)
 Shorthand to await a task with a given timeout.
 
static async Task WithTimeout (this Task task, string taskName, int timeoutMs=10000)
 
static async Task< T > WithRetry< T > (this Task< T > task, Func< T, bool > wasTaskSuccessful, string taskName, int numRetries=3, int retryIntervalMs=1000)
 Perform a task, retrying multiple times if it threw an exception or was not successful.
 
static async void FireAndForget (this Task task, string taskName)
 Performs "fire and forget" (eg. for polling loops or other recurring work) for a task with proper exception logging to prevent errors from becoming unobservable.
 
static async void WithCallbacks< T > (this Task< T > task, Action< T > callbackOnSuccess=null, Action< TaskCanceledException > callbackOnCancel=null, Action< Exception > callbackOnException=null)
 Runs a task in the background and provides synchronous callbacks for different scenarios.
 

Detailed Description

A collection of utilities and extensions to facilitate async programming.

Member Function Documentation

◆ FireAndForget()

static async void FireAndForget ( this Task task,
string taskName )
inlinestatic

Performs "fire and forget" (eg. for polling loops or other recurring work) for a task with proper exception logging to prevent errors from becoming unobservable.

Parameters
taskThe task to fire and forget.
taskNameThe name of the task to use in logs.

◆ WaitUntil()

static async Task WaitUntil ( Func< bool > condition,
int pollingIntervalMs = 100 )
inlinestatic

Shorthand to wait for a condition to be met checking at a given polling interval.

Parameters
conditionA predicate that when true, this method should stop waiting.
pollingIntervalMsThe amount of time to wait between checking the predicate.
Returns
An awaitable task that will complete once the condition is true.

◆ WithCallbacks< T >()

static async void WithCallbacks< T > ( this Task< T > task,
Action< T > callbackOnSuccess = null,
Action< TaskCanceledException > callbackOnCancel = null,
Action< Exception > callbackOnException = null )
inlinestatic

Runs a task in the background and provides synchronous callbacks for different scenarios.

Parameters
taskThe task to run.
callbackOnSuccessInvoked if task completed successfully.
callbackOnCancelInvoked if task was canceled.
callbackOnExceptionInvoked if task failed due to an unhandled exception.
Template Parameters
TThe expected return type of the task.

◆ WithRetry< T >()

static async Task< T > WithRetry< T > ( this Task< T > task,
Func< T, bool > wasTaskSuccessful,
string taskName,
int numRetries = 3,
int retryIntervalMs = 1000 )
inlinestatic

Perform a task, retrying multiple times if it threw an exception or was not successful.

Parameters
taskThe task to try.
wasTaskSuccessfulPredicate determining if it ran successfully.
taskNameThe name of the task to use in logs.
numRetriesThe number of times to retry.
retryIntervalMsThe interval at which to retry.
Template Parameters
TThe return type of the task.
Returns
The latest task result or default if task threw exceptions on every retry.

◆ WithTimeout< T >()

static async Task< T > WithTimeout< T > ( this Task< T > task,
string taskName,
int timeoutMs = 10000 )
inlinestatic

Shorthand to await a task with a given timeout.

Parameters
taskThe task to to await.
taskNameThe name of the task to use in logs.
timeoutMsThe timeout period, after which the task is dropped.
Template Parameters
TThe return type for the task.
Returns
A task with the same return type but an exception is thrown if the timeout period finishes before the task.
Exceptions
TimeoutExceptionThrown if the task times out.

◆ YieldUntil()

static async Task YieldUntil ( Func< bool > condition)
inlinestatic

Shorthand to wait for a condition to be met checking on each frame.

Parameters
conditionA predicate that when true, this method should stop waiting.
Returns
An awaitable task that will complete once the condition is true.

The documentation for this class was generated from the following file: