is `Job#invokeOnCompletion` always invoked indepen...
# coroutines
s
is
Job#invokeOnCompletion
always invoked independent on the result of that Job (e.g. it gracefully completed, or it was cancelled while suspended)?
j
This is explained clearly in the doc of the function: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/invoke-on-completion.html It will always be called, even if the job is actually already completed. The argument of the handler is actually the
cause
of the completion (null for regular completion, an exception if failed, the cancellation exception if cancelled):
Registers handler that is synchronously invoked once on completion of this job. When the job is already complete, then the handler is immediately invoked with the job's exception or cancellation cause or 
null
. Otherwise, the handler will be invoked once when this job is complete.
The meaning of 
cause
 that is passed to the handler:
* Cause is 
null
 when the job has completed normally.
* Cause is an instance of CancellationException when the job was cancelled normally. It should not be treated as an error. In particular, it should not be reported to error logs.
* Otherwise, the job had failed.