Skip to content

Instantly share code, notes, and snippets.

@xavierarpa
Created October 6, 2023 11:17
Show Gist options
  • Save xavierarpa/d3de8a14f4c08085a91cfd92c9299b8b to your computer and use it in GitHub Desktop.
Save xavierarpa/d3de8a14f4c08085a91cfd92c9299b8b to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading.Tasks;
public static class AsyncExtensorUtils
{
public static IEnumerator ToCoroutine(this Task _task)
{
while (!_task.IsCompleted) yield return null;
if (_task.IsFaulted) throw _task.Exception;
}
public static IEnumerator<T> ToCoroutine<T>(this Task<T> _task)
{
while (!_task.IsCompleted) yield return default;
if (_task.IsFaulted) throw _task.Exception;
else yield return _task.Result;
}
public static async Task ToTask(this IEnumerator _ienume)
{
while (_ienume.MoveNext()) await Task.Yield();
}
public static async Task<T> ToTask<T>(this IEnumerator<T> _ienume)
{
while (_ienume.MoveNext()) await Task.Yield();
return _ienume.Current;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment