collectWithTimeout

suspend fun <T> Flow<T>.collectWithTimeout(duration: Duration, collector: FlowCollector<T> = FlowCollector { })

Collects the flow with a specified timeout duration. If the flow takes longer than the provided duration to complete, it throws a TimeoutCancellationException.

Parameters

duration

The maximum time allowed for the flow collection to complete.

collector

An optional FlowCollector for handling the flow's emissions.

Throws

TimeoutCancellationException

if the flow collection takes longer than the specified duration.