using System;
using System.Collections;
using System.Threading.Tasks;
public static class TaskExtensions {
///
/// Converts the Task into an IEnumerator for Unity coroutine usage.
///
/// The Task to convert.
/// An IEnumerator representation of the Task.
public static IEnumerator AsCoroutine(this Task task) {
while (!task.IsCompleted) yield return null;
// When used on a faulted Task, GetResult() will propagate the original exception.
// see: https://devblogs.microsoft.com/pfxteam/task-exception-handling-in-net-4-5/
task.GetAwaiter().GetResult();
}
///
/// Marks a task to be forgotten, meaning any exceptions thrown by the task will be caught and handled.
///
/// The task to be forgotten.
/// The optional action to execute when an exception is caught. If provided, the exception will not be rethrown.
public static async void Forget(this Task task, Action onException = null) {
try {
await task;
}
catch (Exception exception) {
if (onException == null)
throw exception;
onException(exception);
}
}
}