Further reading
While canceling coroutines might seem simple with the help of the associated Job
objects, it's important to note that any cancelation must be cooperative. More specifically, when coroutines perform suspending work based on conditional statements, you must ensure the coroutine is cooperative with respect to canceling.
You can read about this topic, in more detail, in the official documentation: https://kotlinlang.org/docs/cancellation-and-timeouts.html#cancellation-is-cooperative.