Is there any reason why I can't `await()` a corout...
# coroutines
c
Is there any reason why I can't
await()
a coroutine? I thought I would be able to just define my
val result = myRetrofitSuspendingCall
and call
await()
on it. It seems like I need to wrap it in an
async{}
block before I do, but it seems a little counterintuitive. (I'm still new at coroutines)? Is this async block needed because coroutines themselves still read like synchronous code?
o
coroutines don't have to return a value,
async
communicates that you want to return a value
if you don't care about a value and just want to wait for it to complete, use
join
on the Job instead
if you're calling a suspending function, it already is effectively waiting for the result
that's the entire point of the suspend modifier and coroutines in general
c
True. So I basically have to make 4 network calls and wait for all of them to be done. Should I do it as such?
Copy code
val work1 = async { getThingsDone(43) }
                val work2 = async { getThingsDoneAgain(123) }

                val result = computeResult(work1.await(), work2.await())
Or should I use
join
? Also @octylFractal thanks for the help yesterday as well. 😄
o
yes, that works just fine
c
Okay. I feel like coroutines has matured a lot and so I didn't know if I was reading out of date information. I might try join as that seems cool as well, but I guess none of them are objectively better than the other?
o
basically it's just if you want the value or not, when you have a
Deferred<T>
you
await
, when you have a
Job
you
join