Created
October 20, 2023 08:34
-
-
Save xavierarpa/5117a811ec590e678b160be35c405cbf to your computer and use it in GitHub Desktop.
change Task to Coroutine and to Task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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