ProgressFuture

class ProgressFuture<out T>

A running operation that you can await, plus its live progress.

You get one from asyncWithProgress, or from withProgress for an operation that already has a progress flow. Use one per operation that the user sees, such as "open document" or "download file", not one per inner step.

val opening = scope.asyncWithProgress { loadDocument() }
launch { opening.progress.collect { bar.show(it.fraction, it.phase) } }
val document = opening.await()

Compared with Deferred

A ProgressFuture is not a Deferred, because kotlinx-coroutines does not allow other classes to implement Deferred. await and cancel are here. For awaitAll, select or invokeOnCompletion, use deferred.

Watch out

  • progress keeps only the latest value. A slow collector skips reports. To limit UI updates, use a Flow operator such as progress.sample(100.milliseconds).

Properties

Link copied to clipboard
val deferred: Deferred<T>

The running operation, for every Deferred API that this class does not forward.

Link copied to clipboard
val progress: StateFlow<Progress>

The latest Progress. In Compose, read it with progress.collectAsState().

Functions

Link copied to clipboard
suspend fun await(): T

Waits for the result, like Deferred.await. Throws the failure of the operation.

Link copied to clipboard
fun cancel(cause: CancellationException? = null)

Cancels the operation, like Deferred.cancel. progress keeps its last value.

Link copied to clipboard
open override fun toString(): String