cancelAndJoin

suspend fun Job.cancelAndJoin(): Unit (source)

Cancels the job and suspends invoking coroutine until the cancelled job is complete.

This suspending function is cancellable and always checks for the cancellation of invoking coroutine’s Job. If the Job of the invoking coroutine is cancelled or completed when this suspending function is invoked or while it is suspended, this function throws CancellationException.

In particular, it means that a parent coroutine invoking cancelAndJoin on a child coroutine that was started using launch(coroutineContext) { ... } builder throws CancellationException if the child had crashed, unless a non-standard CoroutineExceptionHandler if installed in the context.

This is a shortcut for the invocation of cancel followed by join.