Skip to content

Instantly share code, notes, and snippets.

@xavierarpa
Created October 20, 2023 08:34
Show Gist options
  • Save xavierarpa/5117a811ec590e678b160be35c405cbf to your computer and use it in GitHub Desktop.
Save xavierarpa/5117a811ec590e678b160be35c405cbf to your computer and use it in GitHub Desktop.
change Task to Coroutine and to Task
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