unorderedMapAsync

fun <T, R> Flow<T>.unorderedMapAsync(concurrency: Int, transform: suspend (T) -> R): Flow<R>

Applies the given transformation function transform to each value of the original Flow and emits the results. The processing of items in the flow is concurrent and limited by concurrency level.

The result flow may emit the elements in an unordered manner, which makes this function faster than mapAsync.

Example usage:

flowOf(1, 2, 3)
.unorderedMapAsync(2) { it * 2 }
.collect { println(it) } // Prints 2, 4, 6

Return

A new flow with the transformed items.

Parameters

concurrency

The maximum number of concurrent transformations.

transform

The transformation function to apply to each item.


fun <T, R, P> Flow<T>.unorderedMapAsync(semaphore: suspend CoroutineScope.() -> AsyncSemaphore<P>, transform: suspend (T) -> R): Flow<R>

Applies the given transformation function transform to each value of the original Flow and emits the results. The concurrency is managed by an AsyncSemaphore created by the semaphore suspending function.

The result flow may emit the elements in an unordered manner, which makes this function faster than mapAsync.

Example usage:

val customSemaphore: suspend CoroutineScope.() -> AsyncSemaphore =  { AsyncSemaphore(this, 2) }

flowOf(1, 2, 3)
.unorderedMapAsync(customSemaphore) { it * 2 }
.collect { println(it) } // Prints the results in an unordered manner

Return

A new flow with the transformed items.

Parameters

semaphore

The suspending function that creates an AsyncSemaphore.

transform

The transformation function to apply to each item.