receive

abstract suspend fun receive(): E (source)

Retrieves and removes the element from this channel suspending the caller while this channel isEmpty or throws ClosedReceiveChannelException if the channel isClosedForReceive. If the channel was closed because of the exception, it is called a failed channel and this function throws the original close cause exception.

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 CancellationException.

Cancellation of suspended receive is atomic – when this function throws CancellationException it means that the element was not retrieved from this channel. As a side-effect of atomic cancellation, a thread-bound coroutine (to some UI thread, for example) may continue to execute even after it was cancelled from the same thread in the case when this receive operation was already resumed and the continuation was posted for execution to the thread’s queue.

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 invocation with onReceive clause. Use poll to try receiving from this channel without waiting.