Yielding similar to WWW class?

Hey, I’m trying to figure out how to use these coroutines. Basically I wan’t to create something like the WWW class, where I can just do something like the following,

Request req = new Request();
yield return req.Execute();
Debug.Log("This is executed after the long Execute method is finished processing.");

Any ideas how I might be able to do something like this? I tried implementing my Requests Execute method like so, but it’s not a MonoBehaviour so StartCoroutine does not work.

public IEnumerator MyRoutine()
{
     while(processing)
     {
         yield return null;
     }
}
	
public IEnumerator Execute()
{
	Debug.Log("EXECUTE");
	yield return StartCoroutine("MyRoutine");
}

Is WWW a special class that has some serious Unity magic behind it which is unavailable to the rest of us mortals? Thanks.

Yes, WWW is a special class filled with Unity magic. The MonoBehaviour class also contains these same special magicks, and it exists so that us mortals can use it. In this case, the magic in question is a coroutine scheduler. Either you can write your own, or have a MonoBehaviour member of your class that you can leech the Coroutine functionality off. Is there a MonoBehaviour somewhere in your hierarchy? If so, see if you can get a reference to it into the class that provides your asynchronous functionality, and then whenever you would use StartCoroutine, instead use:

someComponent.StartCoroutine(MyRoutine());

If you are using this object inside of a MonoBehaviour, you can use that monobehaviour's coroutine functionality to schedule the object's coroutines.

Request myRequest = new Request(foo, bar);

StartCoroutine(myRequest.Execute());