Let's say you are within a suspend function... What's the most pragmatic way to cancel your execution? Should you just throw a cancellation exception? Or maybe somehow grab the current job and cancel that?
My use case: I have a third party "cancellation token" i want to check within a suspend function to see if cancellation has been requested. If so, i want to just cancel current suspend function
z
Zach Klippenstein (he/him) [MOD]
01/12/2021, 6:39 PM
To cancel the current coroutine, throw an exception. If you just cancel the job, then your coroutine will continue to execute until something suspends or checks the job status.
r
Rechee Jozil
01/12/2021, 6:40 PM
@Zach Klippenstein (he/him) [MOD] yeah my plan was to do both. Cancel current job and then throw cancellation exception
z
Zach Klippenstein (he/him) [MOD]
01/12/2021, 6:42 PM
Just throwing the exception should be enough. Otherwise the job will logically have two cancellation exceptions, which is confusing.