Hey everyone, I need help with putting something t...
# coroutines
m
Hey everyone, I need help with putting something together but I’m not too sure how to do so. • I want to make 2 API calls in parallel (Call A and Call B) • If Call A finishes first and is Successful, cancel Call B then return result of Call A • If Call B finishes first and is Successful, cancel Call A then return result of Call B I currently have something like this:
Copy code
val callA = async { the call }
val callB = async { the call }

if (callA.await().isSuccess) {
     callB.cancel()
     return callA.await()
}else {
     callA.cancel()
     return callB.await()
}
However, it doesn’t quite get the behavior I want because I’m essentially only waiting for Call A to complete first before I even check on Call B. Can anyone offer some advice, please?
a
use
select {}
and
Deferred.onAwait {}
☝️ 2
n
I find Flow simpler for this:
Copy code
merge(::suspendMethod1.asFlow(), ::suspendMethod2.asFlow()).first()
a
Roman's example from that issue thread is a good shape for something like this
m
Thank you everyone for the advice!