Closed mikecann closed 8 years ago
Are you using the PromiseTimer?
Hi Ashley,
No, not yet. I was hoping there was just a simple solution that would apply to any IPromise.
Mike
If you using PromiseTimer then you could call the Update
function in the coroutine. That might do what you want.
If you are using a Promise to wrap a true asynchronous operation then it's difficult to use that with a coroutine.
If we exposed an IsResolved property from the Promise class then you could poll that in a coroutine. You are welcome to fork the repo add that and see if it solves your problem. If the code looks good we can bring it back into the main repo.
If your problem was the other way around (how to treat coroutines as promises) then I could give you extensive support. I've just been creating examples of how to do that!
Hi Ashley,
Okay no worries, I was thinking that perhaps this would do it:
namespace RSG
{
public static class PromiseExtensions
{
public static IEnumerator ToEnumerator(this IPromise promise)
{
var done = false;
promise
.Done(() => done = true);
while(!done)
yield return null;
}
public static IEnumerator ToEnumerator<T>(this IPromise<T> promise)
{
var done = false;
promise
.Done(_ => done = true);
while (!done)
yield return null;
}
}
}
What do you think?
Awesome. That looks like it should work!
Ill see if it works in my game then fork and pull request, unless you dont want it in the lib / want to just include it yourself?
@mikecann do you try make a promise under Unity Main thread for some UnityApi action?
@LoranceChen Im not sure what you mean by that. You could make this run on the main thread if you "Loomed" it
Hi,
Is there a way to convert a Promise to a Coroutine (to keep consistency with for legacy code)?
Cheers, Mike