...

/

Solution: Cancellation of a Job

Solution: Cancellation of a Job

See the solution to the challenge concerning cancellation of a job.

We'll cover the following...

Solution

suspend fun main(): Unit = coroutineScope {
   val job = Job()
   launch(job) {
       try {
           repeat(1_000) { i ->
               delay(200)
               println("i = $i")
           }
       } catch (e: CancellationException) {
           println(e)
           throw e
       }
   }
   delay(1500)
   job.cancelAndJoin()
   println("Canceled successfully")
}
Complete code for the solution

Here is a line–by–line explanation of the code above:

...