getOrNull

fun getOrNull(): T?(source)

Returns the encapsulated T if the operation succeeded, or null if it failed.

For non-nullable T, the following code can be used to handle the result:

val result = channel.tryReceive()
val value = result.getOrNull()
if (value == null) {
if (result.isClosed) {
println("The channel is closed.")
if (result.exceptionOrNull() != null) {
println("The reason: ${result.exceptionOrNull()}")
}
}
return
}
println("Successfully received the value $value")

If T is nullable, getOrThrow together with isSuccess is a more reliable way to handle the result.