Created
October 6, 2023 11:17
-
-
Save xavierarpa/d3de8a14f4c08085a91cfd92c9299b8b to your computer and use it in GitHub Desktop.
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