altianogerung
08/26/2023, 9:54 AMlaunch
and delay
functions inside coroutine?
fun main() = runBlocking { // this: CoroutineScope
launch { // launch a new coroutine and continue
delay(1000L) // non-blocking delay for 1 second (default time unit is ms)
println("World!") // print after delay
}
println("Hello") // main coroutine continues while a previous one is delayed
}
reactormonk
08/26/2023, 9:55 AMsuspend
? Coroutines?altianogerung
08/26/2023, 9:56 AMlaunch
and delay
methods, why we can access them?reactormonk
08/26/2023, 9:57 AMlauch
is basically
kotlin
runBlocking { scope ->
scope.launch { ...
AFAIKreactormonk
08/26/2023, 9:57 AMaltianogerung
08/26/2023, 10:01 AMreactormonk
08/26/2023, 10:01 AMaltianogerung
08/26/2023, 10:22 AMCoroutineScope
🤔🤷♂️Riccardo Lippolis
08/26/2023, 10:41 AMrunBlocking
function takes such an argument (the code block that you supply) in the form block: suspend CoroutineScope.() -> T
. So inside the code block you provide to the runBlocking
function, CoroutineScope
becomes the implicit this
, meaning that you can call functions from the CoroutineScope
class, like launch
and delay
. The runBlocking
function then runs your code block, in the context of an instance of CoroutineScope
.Loney Chou
08/26/2023, 4:32 PMdelay
is just a regular suspend function.