https://kotlinlang.org logo
Title
p

projectmoon

06/05/2019, 5:03 PM
is there any point to doing something like
lauch { val result = async { doStuff() }.await() }
, assuming
doStuff()
is a suspend fun? or would it make more sense to simply call
doStuff()
inside the launch block without
async
?
z

Zach Klippenstein (he/him) [MOD]

06/05/2019, 5:20 PM
just call
doStuff()
inside
launch
, that
async
isn’t doing anything.
p

projectmoon

06/05/2019, 5:27 PM
i figured as much. where is async best applied, then?
i guess when you have multiple suspend functions that you want to wait for simultaneously, and/or they are long-running?
:yes: 2
j

Jason Ostrander

06/05/2019, 5:40 PM
async
is used when you want to run code in parallel
So, if you wanted to issue two network requests at the same time, then wait for both to complete, you could start them both in their own
async
blocks, then await on both of the returned Deferreds
p

projectmoon

06/05/2019, 5:44 PM
sounds right
most of my async knowledge comes from nodejs callbacks, so i still think that way heavily
d

Dico

06/05/2019, 6:20 PM
async
shouldnt be user very often at all, as a generale type of thumb.
withContext
should commonly be used instead, when you invoke
await
immediately anyway