Kotlin channel - is there a non-suspending receive?

Viewed 176

Apparently if we call channel.receive() against an empty channel, it suspends, which is exactly as documented here in official docs. Is there a way to immediately return a null instead, like what ConcurrentLinkedQueue's poll() does?

1 Answers

Yes, we can use tryReceive():

channel.tryReceive().getOrNull()
Related