#Launch vs Async in ViewModel

1 messages · Page 1 of 1 (latest)

tight sunBOT
#

@lucid moth has a question:

refactorall

how do i know when i want a launch vs a async?
because in the viewmodel i wouldve done async because presumably i wanted the value from the repo call to update the state but the docs show doing launch

#

<@&987246399047479336> please have a look, thanks.

boreal wigeon
#

im sorry, could u provide some context. what exactly are u talking about?

barren creek
lucid moth
#

no you guys dont use coroutines?

boreal wigeon
#

this is a java server. so without extra context people are confused

barren creek
# lucid moth no you guys dont use coroutines?

Coroutines are a kotlin feature. This is a java discord. So that you are talking about kotlin coroutines is context that cant be assumed.

Anyway you use launch if you want to perform a side effect asynchronously (e.g. updating state) and async if you want to calculate a value asynchronously that you can later access (e.g. 2 asynchronous http calls where you want to continue with both results).

tight sunBOT
#

Changed the category to Kotlin.

#

<@&1008423204219531294> please have a look, thanks.

main bobcat
#

You should avoid returning Deferred from a function. A plain suspend function that returns a value is plenty!

#

Async is for when you want to multitask multiple operations within one function, and should be added inside a function. And make sure to await it before the end of the function. Don't leak the Deferred outside of the scope of the place that async was called.