I’m noticing a little bit of inconsistent behavior...
# coroutines
r
I’m noticing a little bit of inconsistent behavior with
invokeOnCompletion
. Given the following
Copy code
launch(Dispatchers.Default) {
  val theJob = async(<http://Dispatchers.IO|Dispatchers.IO>) {}

  // do some work

  theJob.invokeOnCompletion { println() }
}
If
do some work
takes longer than
theJob
to complete, the handler block runs on
Dispatchers.Default
, whereas if
theJob
takes longer than
do some work
, the handler block runs on
<http://Dispatchers.IO|Dispatchers.IO>
.
d
From the docs (https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/invoke-on-completion.html):
Note: Implementation of CompletionHandler must be fast, non-blocking, and thread-safe. This handler can be invoked concurrently with the surrounding code. There is no guarantee on the execution context in which the handler is invoked.
💯 1
r
Thank you