receive

abstract suspend fun receive(): E(source)

Retrieves and removes an element from this channel if it's not empty, or suspends the caller while the channel is empty, or throws a ClosedReceiveChannelException if the channel is closed for receive. If the channel was closed because of an exception, it is called a failed channel and this function will throw the original close cause exception.

This suspending function is cancellable. If the Job of the current coroutine is cancelled 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 receive 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.

This suspending function is cancellable: if the Job of the current coroutine is cancelled while this suspending function is waiting, this function immediately resumes with CancellationException. There is a prompt cancellation guarantee: even if receive managed to retrieve the element from the channel, but was cancelled while suspended, CancellationException will be thrown. See suspendCancellableCoroutine for low-level details.

Because of the prompt cancellation guarantee, some values retrieved from the channel can become lost. 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 onReceive clause. Use tryReceive to try receiving from this channel without waiting.