https://kotlinlang.org logo
Title
j

julioyg

11/05/2018, 11:48 AM
Hi: if I got a function like
fun doSomeStuff() : Deferred<String> = async { /*doing something*/}
, should that be an extension function of a
coroutineContext
as it starts a coroutine?
g

gildor

11/05/2018, 11:56 AM
Yes, extension function for CoroutineContext is right solution, but in general this approach is not recommended, better to use suspend function and allow user of it wrap to async if some concurrent calls are required
See coroutunes guide for details and rationale
j

julioyg

11/05/2018, 1:52 PM
would
suspend fun doSomeStuff() = withContext {}
be better, that doesn't launch a coroutine but changes the context of the existing one, right?
thanks BTW 🙂
g

gildor

11/05/2018, 3:53 PM
Yes, suspend + wittContext is recommended way See for instance this article https://medium.com/@elizarov/blocking-threads-suspending-coroutines-d33e11bf4761
j

julioyg

11/05/2018, 3:59 PM
great, thanks