One way to cooperate is to call yield() from time to time from within your suspend function. This not only lets Kotlin switch execution to some other coroutine, but it will check to see if the job has been canceled. If the job has been canceled, yield() will throw the CancellationException necessary to exit the lambda expression.

You can learn more about this in:
Tags:
Run Edit