Package-level declarations

Suspend helpers that handle cancellation correctly.

Start here:

Types

Link copied to clipboard
class SingleFlight<K, T>

Makes callers that ask for the same key at the same time share one run of the work.

Link copied to clipboard
class SuspendLazy<T>

A lazy value whose initializer can suspend. Create one with suspendLazy.

Functions

Link copied to clipboard
suspend fun <T> Iterable<T>.parallelForEach(concurrency: Int = Int.MAX_VALUE, action: suspend (T) -> Unit)

Like forEach, but runs action for many elements at the same time. Returns when all are done.

Link copied to clipboard
suspend fun <T, R> Iterable<T>.parallelMap(concurrency: Int = Int.MAX_VALUE, transform: suspend (T) -> R): List<R>

Like map, but runs transform for many elements at the same time. The results keep the input order.

Link copied to clipboard
suspend fun <T> retryWithBackoff(times: Int, initialDelay: Duration, factor: Double = 2.0, maxDelay: Duration = Duration.INFINITE, retryIf: (Throwable) -> Boolean = { true }, block: suspend (attempt: Int) -> T): T

Runs block until it succeeds, at most times times, with a longer pause after each failure.

Link copied to clipboard
suspend fun <T> runCatchingCancellable(block: suspend () -> T): Result<T>

Like runCatching, but safe in suspend code: it never swallows the cancellation of the caller.

Link copied to clipboard
suspend fun <T, R> T.runCatchingCancellable(block: suspend T.() -> R): Result<R>

The receiver form of runCatchingCancellable: block runs with this as its receiver.

Link copied to clipboard
fun <T> suspendLazy(initializer: suspend () -> T): SuspendLazy<T>

Like lazy, but the initializer can suspend. Read the value with SuspendLazy.get.