iex
04/19/2020, 9:12 PMoverride fun foo(): List<String> =
runBlocking {
fooSuspended()
}
suspend fun fooSuspended(): List<String> =
coroutineScope {
val res: List<Deferred<String?>> = (1..100).map { n ->
async {
doWork()
}
}
res.mapNotNull { it.await() }
}
fun doWork(): String? {
Thread.sleep(500)
return "hi"
}
octylFractal
04/19/2020, 9:14 PMrunBlocking(Dispatcher.Default) { ... }
iex
04/19/2020, 9:14 PMoctylFractal
04/19/2020, 9:14 PMasync(Dispatcher.Default) { ... }
and achieve a similar effectiex
04/19/2020, 9:15 PMoctylFractal
04/19/2020, 9:15 PMDispatchers
? been a bitiex
04/19/2020, 9:16 PMDispatcher
was from ok httpiex
04/19/2020, 9:17 PMDominaezzz
04/19/2020, 10:06 PM<http://Dispatchers.IO|Dispatchers.IO>
since you're blocking with Thread.sleep
.iex
04/19/2020, 10:14 PMstreetsofboston
04/19/2020, 10:17 PMres.mapNotNull { it.await() }
, what if you tried res.awaitAll().mapNotNull()
?
It shouldn’t matter much, since all `async`s are kicked off at the same time, but I’m curious 🙂iex
04/19/2020, 10:18 PMIO
anyway and confirm that it's not good for computations. It duplicated the time 😅Dominaezzz
04/19/2020, 10:20 PMiex
04/19/2020, 10:20 PMawaitAll().mapNotNull()
doesn't change the timeiex
04/19/2020, 10:20 PMDominaezzz
04/19/2020, 10:21 PMiex
04/19/2020, 10:24 PMiex
04/19/2020, 10:25 PMiex
04/19/2020, 10:27 PMDominaezzz
04/19/2020, 10:43 PM