abstract suspend fun receiveCatching(): ChannelResult<E>

Retrieves and removes an element from this channel if it's not empty, or suspends the caller while this channel is empty. This method returns ChannelResult with the value of an element successfully retrieved from the channel or the close cause if the channel was closed. Closed cause may be null if the channel was closed normally. The result cannot be failed without being closed.

This suspending function is cancellable. If the Job of the current coroutine is cancelled or completed while this function is suspended, this function immediately resumes with a CancellationException. There is a prompt cancellation guarantee. If the job was cancelled while this function was suspended, it will not resume successfully. The receiveCatching call can retrieve the element from the channel, but then throw CancellationException, thus failing to deliver the element. See "Undelivered elements" section in Channel documentation for details on handling undelivered elements.

Note that this function does not check for cancellation when it is not suspended. Use yield or CoroutineScope.isActive to periodically check for cancellation in tight loops if needed.

This function can be used in select invocations with the onReceiveCatching clause. Use tryReceive to try receiving from this channel without waiting.


common source
Link copied to clipboard