dstarcev
10/22/2018, 9:20 AMJonathan
10/22/2018, 9:20 AMsuspend inline fun <T, R> Iterable<T>.mapAsync(
crossinline transform: suspend (T) -> R
) = coroutineScope {
map { async { transform(it) } }.map { it.await() }
}
mapAsync
to suspend until all jobs are completedmapAsync
to throw as soon as one job fail.gildor
10/22/2018, 9:21 AMJonathan
10/22/2018, 9:21 AMdstarcev
10/22/2018, 9:22 AMgildor
10/22/2018, 9:23 AMJonathan
10/22/2018, 9:24 AMcoroutineScope
more explicitgildor
10/22/2018, 9:24 AMis it correct for a function to have it’s own scope ignoring whatever it is on the caller siteNo,
coroutineScope
uses coroutineContext
of outer scopedstarcev
10/22/2018, 9:27 AMsuspend inline fun <T, R> Iterable<T>.mapAsync1(
crossinline transform: suspend (T) -> R
) = coroutineScope { map { async { transform(it) } }.awaitAll() }
suspend inline fun <T, R> Iterable<T>.mapAsync2(
crossinline transform: suspend (T) -> R
) = map { GlobalScope.async { transform(it) } }.awaitAll()
Jonathan
10/22/2018, 9:27 AMgildor
10/22/2018, 9:28 AMdstarcev
10/22/2018, 9:28 AMsuspend inline fun <T, R> Iterable<T>.mapAsync2(
crossinline transform: suspend CoroutineScope.(T) -> R
) = map { GlobalScope.async { transform(it) } }.awaitAll()
gildor
10/22/2018, 9:28 AMJonathan
10/22/2018, 9:29 AMgildor
10/22/2018, 9:29 AMdstarcev
10/22/2018, 9:30 AMgildor
10/22/2018, 9:31 AMwithContext(<http://Dispatchers.IO|Dispatchers.IO>)
dstarcev
10/22/2018, 9:35 AMrunBlocking(<http://Dispatchers.IO|Dispatchers.IO>) {
...
}
gildor
10/22/2018, 9:54 AMdstarcev
10/22/2018, 9:57 AMgildor
10/22/2018, 9:58 AM