https://kotlinlang.org logo
Title
d

dave08

02/03/2019, 6:05 PM
I guess it would have been nice to have a
getCompletedOrNull()
that returns a null instead of throwing... so currently the only way is to
runCatching { deferred.getCompleted() }.getOrElse { defaultValue }
?
a

altavir

02/03/2019, 6:56 PM
You can check
isCompleted
instead
if(deferred.isCompleted()) getCompleted() else defaultValue
c

cbruegg

02/03/2019, 8:22 PM
So here's your
poll
method:
fun <T: Any> Deferred<T>.poll() = if (isCompleted) getCompleted() else null
d

dave08

02/03/2019, 8:26 PM
Thanks that's true, probably better than catching the exception, but I wonder if it would be useful in the lib itself? Why is it there in Channels but not in Deferred?