I have a coroutine as follows:
IEnumerator CountingCoroutine()
{
while (true)
{
DoSomething();
yield return new WaitForSeconds(1);
}
}
I need to call some async function within it. If I wrote it as follows, the compiler complains:
async IEnumerator CountingCoroutine()
{
while (true)
{
DoSomething();
await foo();
yield return new WaitForSeconds(1);
}
}
What is the proper wait to call some async functions per second? P.S. I could abandon the whole coroutine function and use update with time counting mechanism, and mark the update function as async. But I'm not sure if it's safe or recommended.
Solution with recommendations from @derHugo:
IEnumerator CountingCoroutine()
{
while (true)
{
DoSomething();
var task = Task.Run(foo);
yield return new WaitUntil(()=> task.IsCompleted);
yield return new WaitForSeconds(1);
}
}