currentCoroutineContext

Returns the current CoroutineContext retrieved by using kotlin.coroutines.coroutineContext.

This function is an alias to avoid name clash with CoroutineScope.coroutineContext:

// ANTIPATTERN! DO NOT WRITE SUCH CODE
suspend fun CoroutineScope.suspendFunWithScope() {
// Name of the CoroutineScope.coroutineContext in 'this' position, same as `this.coroutineContext`
println(coroutineContext[CoroutineName])
// Name of the context that invoked this suspend function, same as `kotlin.coroutines.coroutineContext`
println(currentCoroutineContext()[CoroutineName])
}

withContext(CoroutineName("Caller")) {
// Will print 'CoroutineName("Receiver")' and 'CoroutineName("Caller")'
CoroutineScope(CoroutineName("Receiver")).suspendFunWithScope()
}

This function should always be preferred over kotlin.coroutines.coroutineContext property even when there is no explicit clash.

Usage example:

// log an event while specifying the current coroutine
println("[${currentCoroutineContext()[Job]}] Hello, World!")

Consider using coroutineScope instead of this function to obtain not only the current context but also a new CoroutineScope that allows easily launching coroutines and waiting for their completion.