This is, in effect, "hello, world" in the realm of coroutines. If you run this, you will see one message printed immediately and the other after a short delay.
There are five main elements when working with coroutines:
A
CoroutineScope
, such as theGlobalScope
that is used hereA coroutine builder, such as the
launch()
function onGlobalScope
Dispatchers, such as
Dispatchers.Default
andDispatchers.Main
suspend
functions, for marking code that can be suspended safely so other code can runA
CoroutineContext
, such as the one created viawithContext()
, to change which dispatcher should be used for some code
All of these will be explored in upcoming lessons.
You can learn more about this in:
Tags: