Search
K

VyTask (Async)

Most functions in the API will return a VyTask object that may or may not contain data of a specific type (VyTask & VyTask<T>). These are built on top of the .NET Tasks but offer you a promise-like way of handling the async operations.
VyTasks can be awaited, or you can set callbacks that fire on various events.

VyTask

A VyTask will notify you the moment an async procedure is completed. It contains some basic properties that give you more information (VyTaskResult):
  • Success (bool) > Whether the task was completed successfully or not
  • Cancelled (bool) > Whether the task was canceled
  • Exception (Exception) > In case of unsuccessful or canceled, this field will give you more info about the reason
  • Data (T) > The data that should be returned once the task is finished
It is possible that future releases of the API will further extend the VyTaskResult. (This could include additional API response information like Pagination)

VyTask Usage

The following snippet will show you 4 ways of retrieving all the wallets
//1. Default Await
VyTaskResult<VyWalletDto[]> walletResult = await VenlyAPI.Wallet.GetWallets();
if (walletResult.Success) Debug.Log($"YAY - retrieved {walletResult.Data.Length} wallet(s)");
else Debug.LogException(walletResult.Exception);
//2. Result Await (will throw on exception)
VyWalletDto[] wallets = await VenlyAPI.Wallet.GetWallets().AwaitResult();
Debug.Log($"YAY - retrieved {wallets.Length} wallet(s)");
//3. Promise-Like (OnComplete)
VenlyAPI.Wallet.GetWallets()
.OnComplete(result =>
{
if(result.Success) Debug.Log($"YAY - retrieved {walletResult.Data.Length} wallet(s)");
else Debug.LogException(result.Exception);
});
//4. Promise-Like (OnSuccess/OnFail)
VenlyAPI.Wallet.GetWallets()
.OnSuccess(wallets => Debug.Log($"YAY - retrieved {walletResult.Data.Length} wallet(s)"))
.OnFail(Debug.LogException);

VyTask Creation

Next to usage in the API itself, you can of course also create and return your own VyTasks. The moment you define a new VyTask, a VyTaskNotifier object is created. This notifier object is used to signal the state of the task.
The following (simple) snippet shows you how to create a VyTask, and use its corresponding VyTaskNotifier to signal for completion.
//using ASYNC/AWAIT
public async void ExecuteTaskAsync()
{
var result = await CancelIfNoWallets(); //CancelIfNoWallets code below
if (result.Success) Debug.Log("Wallets Available");
else if (result.Cancelled) Debug.Log("No Wallets, abort!");
else Debug.LogException(result.Exception);
}
//without ASYNC/AWAIT
public void ExecuteTask()
{
CancelIfNoWallets() //CancelIfNoWallets code below
.OnSuccess(() => Debug.Log("Wallets Available"))
.OnCancel(() => Debug.Log("No Wallets, abort!"))
.OnFail(Debug.LogException)
.Finally(() => Debug.Log("Whatever happens, execute this after completion..."));
}
//Simple Async Function that Cancels if there are no wallets present
public VyTask CancelIfNoWallets()
{
//Non Generic VyTask
VyTaskNotifier taskNotifier = VyTask.Create(); //or VyTask<T>.Create()
//Async Task
VenlyAPI.Wallet.GetWallets()
.OnSuccess(wallets =>
{
if(wallets.Length > 0) taskNotifier.NotifySuccess(); //Wallets = SUCCESS
else taskNotifier.NotifyCancel(); //No Wallets = CANCEL
})
.OnFail(ex =>
{
taskNotifier.NotifyFail(ex); //Something went wrong... = EXCEPTION
});
return taskNotifier.Task;
}
Last modified 3mo ago