hasNext

abstract suspend operator fun hasNext(): Boolean

Returns true if the channel has more elements, suspending the caller while this channel is empty, or returns false if the channel is closed for receive without a cause. It throws the original close cause exception if the channel has failed.

This function retrieves and removes an element from this channel for the subsequent invocation of next.

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

Sources

common source
Link copied to clipboard