https://kotlinlang.org logo
Title
i

Ismaïl

03/26/2023, 5:27 PM
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

franztesca

03/26/2023, 5:43 PM
coroutineScope { launch(IO) { ... } }
Is not really idiomatic. It does the same as
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

Ismaïl

03/26/2023, 5:48 PM
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

franztesca

03/26/2023, 6:24 PM
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.