How can we use the result of a first routing to ca...
# coroutines
s
How can we use the result of a first routing to call the 2nd one (reformulating my previous question)
b
you need something like this?
Copy code
suspend fun getFirstResult() : Data? = try {
    getData().await()
} catch(e: Exception) {
    null
}

suspend fun getSecondResult(data: Data) {}

fun getContent() = launch {
    getFirstResult()?.let{ getSecondResult(it) }
}
s
You’d probably want to change
getContent
a bit:
Copy code
fun CoroutineScope.getContent() = async { 
    getFirstResult()?.let { getSecondResult(it) }
}
s
I see
Yea i would probably use async instead of launch
b
depends on what you want to do, no @streetsofboston? Example: I call that from my view that does not have any dependency of coroutines or anything like that and in my
getContent
I have at the final a
handleResponse()
to make the communication to my view with states, then my
getContent
needs to launch, or am I missing something?
s
in my case, i’m showing a spinner when the user taps on a button, it will stay visible until i have the result of the 2nd function
s
Yes, it depends on the use-case, but the original poster used
async
. And there’s a need of a
CoroutineScope
to call
async
(or
launch
).
s
yes , i’ll be using GlobalScope
s
Be very very careful using GlobalScope, though.
s
can you elaborate?
s
You lose structured concurrency when using GlobalScope. Let me find the article that explains this (better than discussing it here in a thread)
s
Okay thank you @bloder ++
b
this is a good article that talks about it
s
s
I’m also using a DI
s
That should not matter. 🙂 But you can use DI to inject a TestCoroutineScope (or a TestCoroutineDispatcher) for your unit-tests while injecting actual Dispatchers (IO, Default, Main) for your actual app.
s
ok ok great