Pablo
08/25/2022, 10:13 AMJob
and then if the first finishes first cancel the second Job
to stop doing the work, also I think the way to do it is using launch{}
right? Instead of async {}
simon.vergauwen
08/25/2022, 10:14 AMselect
for this, or something like raceN
from Arrow Fx.
https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.selects/select.html
https://arrow-kt.io/docs/fx/parallel/#racing-parallel-operationsPablo
08/25/2022, 10:15 AMsimon.vergauwen
08/25/2022, 10:19 AMraceN
in Arrow which is build on top of select
.
https://github.com/arrow-kt/arrow/blob/706a48f2fd9fdf07b4bb3f982122a9e42c29c9b5/ar[…]x-coroutines/src/commonMain/kotlin/arrow/fx/coroutines/Race2.ktPablo
08/25/2022, 10:19 AMsuspend fun getFaster(): Int = coroutineScope {
select<Int> {
async { getFromServer() }.onAwait { it }
async { getFromDB() }.onAwait { it }
}.also {
coroutineContext.cancelChildren()
}
}
This would work?simon.vergauwen
08/25/2022, 10:19 AMPablo
08/25/2022, 10:22 AMsimon.vergauwen
08/25/2022, 10:23 AMFrancesc
08/25/2022, 7:56 PMNick Allen
08/26/2022, 3:32 AMmerge(::foo.asFlow(), ::bar.asFlow()).first()
If you have suspend methods foo and bar