genovich
04/10/2021, 5:25 PMDeferred
in select
when the first one returns value?
select {
deferred1.onAwait { it }
deferred2.onAwait { it }
}
elizarov
04/10/2021, 5:34 PMgenovich
04/10/2021, 6:31 PMelizarov
04/10/2021, 7:10 PMgenovich
04/10/2021, 8:20 PMgenovich
04/11/2021, 7:27 AMasync
with job
, but think that this is more reliable than cancel every Deferred
.
val job = Job()
val select = select {
async(job) { suspend1() }.onAwait { it }
async(job) { suspend2() }.onAwait { it }
}
job.cancelChildren()
elizarov
04/12/2021, 8:58 AMselect
throws CancellationException
, but both async jobs continue to workelizarov
04/12/2021, 9:03 AMval result = coroutineScope {
select {
async { suspend1() }.onAwait { it }
async { suspend2() }.onAwait { it }
}.also {
coroutineContext.cancelChildren()
}
}
genovich
04/12/2021, 9:04 AMjob
to current context, and totally forget about exceptions.
Your solution looks great, thank you.