Hank
07/31/2021, 8:45 AMJan Skrasek
07/31/2021, 9:40 AMHank
07/31/2021, 9:42 AMRichard Gomez
08/01/2021, 12:34 AMselect { }
function. Perhaps something like:
// Ugly code
val requests = List<Deferred<String>>(5) {
delay(Random.nextInt(0, 1000).toLong())
async { "Response #$it" }
}
val response = select<String> {
requests.withIndex().forEach { (index, deferred) ->
deferred.onAwait { answer ->
requests.forEach { it.cancel() }
"Deferred $index produced answer '$answer'"
}
}
}
Nick Allen
08/01/2021, 4:19 AMFlow
withTimeout(limit) {
merge(::func1.asFlow(),::func2.asFlow(),... ). first()
}
(formatting not working on phone)Jan Skrasek
08/01/2021, 11:45 AMRichard Gomez
08/01/2021, 4:55 PMyour's example seems reasonable, what's not working?Nothing in particular, just confirming that I have the right idea. š Is there a more idiomatic way to cancel a list of jobs than
requests.forEach(Job::cancel)
?
I suppose you could run them in an ad-hoc context and cancel that once the first is completed.