Orhan Tozan
03/10/2020, 10:17 PMawaitAll()
I need something like awaitAllFirstFinished()
Mark Murphy
03/10/2020, 11:20 PMselect()
will be what you want: https://kotlinlang.org/docs/reference/coroutines/select-expression.htmlaraqnid
03/11/2020, 5:57 PMfun adapt(out: CompletableDeferred<T>, allJobs: Collection<Jobs>) = launch {
val result = deferred.await()
out.complete(result)
allJobs.forEach { it.cancel() }
}
val out = CompletableDeferred<T>()
allDeferreds.map { adapt(out, allDeferreds) }.joinAll()
error handling and correct scoping to avoid cancellation causing armageddon is left out 😉
using select
is probably simpler, though