I’d like to exit the running awaited function mid-execution.
All i’ve seen people do, is to throw new Exception (which you can handle after using try/catch), but i’d like to handle those separately.
NOTE this is copy of this and will be updated accordingly.
I don’t really use async / await methods. However you should be able to simply use
return; for methods without a return type (So the return type is void) or
return somevalue;if you have an async method that returns a
Task<SomeType>. This works the same as with a normal method.
Note that in “normal” coroutines to break a coroutine you have to use
yield break; Of course an async method that should return a value always have to return a value when you exit / finish it. The only “exception” is when you throw an “exception” but that means when the exception is used to leave the async method the calling code has to handle it. Keep in mind that try-catch and exceptions should never be used as a control flow mechanism. They are quite expensive and should only used for exceptional states which makes it impossible to continue because you don’t know what to do.
If you want a more detailed answer, you should include you actual code, when and why you want to exit the method and at which point.