isSuccess
Whether the operation succeeded.
If this returns true
, the operation was successful. In this case, getOrNull and getOrThrow can be used to retrieve the value.
If this returns false
, the operation failed. isClosed can be used to determine whether the operation failed because the channel was closed (and therefore retrying the operation is meaningless).
val result = channel.tryReceive()
if (result.isSuccess) {
println("Successfully received the value ${result.getOrThrow()}")
} else {
println("Failed to receive the value.")
if (result.isClosed) {
println("The channel is closed.")
if (result.exceptionOrNull() != null) {
println("The reason: ${result.exceptionOrNull()}")
}
}
}
Content copied to clipboard
isFailure is a shorthand for !isSuccess
. getOrNull can simplify isSuccess followed by getOrThrow into just one check if T is known to be non-nullable.