Hi: if I got a function like `fun doSomeStuff() : ...
# coroutines
j
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
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
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
Yes, suspend + wittContext is recommended way See for instance this article https://medium.com/@elizarov/blocking-threads-suspending-coroutines-d33e11bf4761
j
great, thanks