suspend fun Job.cancelAndJoin()

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

This suspending function is cancellable and always checks for a cancellation of the 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 throws CancellationException if the child had failed, since a failure of a child coroutine cancels parent by default, unless the child was launched from within supervisorScope.

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


common source
Link copied to clipboard