Skip to content

Instantly share code, notes, and snippets.

@adammyhre
Created August 25, 2024 18:29
Show Gist options
  • Save adammyhre/40e7c968edda098fdebb2adbe806c911 to your computer and use it in GitHub Desktop.
Save adammyhre/40e7c968edda098fdebb2adbe806c911 to your computer and use it in GitHub Desktop.
A helper class for UniTask that implements a WhenAllSettled method, allowing all tasks to complete and capturing both successful results and exceptions.
using System;
using Cysharp.Threading.Tasks;
/// <summary>
/// Provides helper methods for working with UniTask, including methods to await multiple tasks
/// and capture their results or exceptions.
/// </summary>
public class UniTaskHelpers {
/// <summary>
/// Awaits two UniTasks and returns a tuple containing the results or exceptions for each task.
/// Ensures that both tasks complete before returning, even if one or both fail.
/// </summary>
/// <typeparam name="T1">The result type of the first task.</typeparam>
/// <typeparam name="T2">The result type of the second task.</typeparam>
/// <param name="task1">The first task to await.</param>
/// <param name="task2">The second task to await.</param>
/// <returns>A tuple containing the results or exceptions of both tasks.</returns>
public static async UniTask<(Result<T1> Task1Result, Result<T2> Task2Result)> WhenAllSettled<T1, T2>(UniTask<T1> task1, UniTask<T2> task2) {
var task1Result = await WrapTaskResult(task1);
var task2Result = await WrapTaskResult(task2);
return (task1Result, task2Result);
}
static async UniTask<Result<T>> WrapTaskResult<T>(UniTask<T> task) {
try {
var result = await task;
return new Result<T>(result, null);
}
catch (Exception ex) {
return new Result<T>(default, ex);
}
}
public struct Result<T> {
public T Value { get; }
public Exception Exception { get; }
public bool IsSuccess => Exception == null;
public Result(T value, Exception exception) {
Value = value;
Exception = exception;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment