https://kotlinlang.org logo
#coroutines
Title
# coroutines
e

evan

01/18/2019, 8:54 PM
Hello, I'm using IntelliJ to try out coroutines on the JVM. I have a pretty simple method that calls
GlobalScope.launch
, awaits a deferred value, prints its contents, and then outside of the launch method, prints a concluding String. All of this is working great. The process is still running when these two print tasks complete however. What is keeping it alive, and can I gracefully shutdown when all my work has been completed? Edit: Updated code block from Louis' comments
Copy code
suspend fun main() {
    val api = createRestApi<RestApi>("<https://catfact.ninja/>", okHttpClient(), moshi)

    coroutineScope {
        val facts = api.getFacts().await()
        println(facts.data)
    }
    println("That's all")
}
l

louiscad

01/18/2019, 9:14 PM
You should wrap with
coroutineScope { … }
instead of using
GlobalScope
Then you'd not event need
launch
and
join
e

evan

01/18/2019, 9:17 PM
I've updated the function and it looks much nicer (and is now structured). The process is still running after "Thats all" is printed however
s

streetsofboston

01/18/2019, 11:36 PM
✔️ 1
5 Views