Hey guys I’m trying to understand how `suspendCoro...
# coroutines
i
Hey guys I’m trying to understand how
suspendCoroutine
works, I tried to implement an example, here is the GitHub Gist link Does this code make sense ? it works but I was wondering if it can be improved somehow Thanks in advance
f
Copy code
coroutineScope { launch(IO) { ... } }
Is not really idiomatic. It does the same as
Copy code
withContext(IO) { ... }
Also, IO context is used to bridge blocking code to suspend functions. In your case, you are using an asynchronous function underneath, so there is no need of the IO context, so no need of the
withContext
too
i
Using
withContext
doesn’t create a
CoroutineScope
, does it ? If it doesn’t, then
coroutineScope
is better, so we have children coroutines and not sibling. Am I wrong ?
f
It does (look at the receiver in the lambda of withContext). But you don't even need a coroutine scope as you don't really need to launch any coroutine in
sendRequestAndPrintResultSuspendable
, because you can just call the other suspending function directly.