hasNext
Prepare an element for retrieval by the invocation of next.
If the element that was retrieved by an earlier hasNext call was not yet consumed by next, returns
true
.If the channel has an element available, returns
true
and removes it from the channel. This element will be returned by the subsequent invocation of next.If the channel is closed for receiving without a cause, returns
false
.If the channel is closed with a cause, throws the original close cause exception.
If the channel is not closed but does not contain an element, suspends until either an element is sent to the channel or the channel gets closed.
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 hasNext retrieves the element from the channel during its operation, 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 the "Undelivered elements" section in the Channel documentation for details on handling undelivered elements.
Note that this function does not check for cancellation when it is not suspended, that is, if the next element is immediately available. Use ensureActive or CoroutineScope.isActive to periodically check for cancellation in tight loops if needed.