oday
09/04/2022, 10:39 PMMain
Dispatchers called there? there’s no Main thread like UI dispatcher in kotlin itself https://hastebin.com/eyolacixis.propertiesoday
09/04/2022, 10:42 PModay
09/04/2022, 10:47 PModay
09/04/2022, 10:48 PMJoffrey
09/05/2022, 5:59 AMGlobalScope.launch
anyway there, because the main()
method would then complete and your program would exit before the coroutine even runs at all.
If you want to run a coroutine from main()
on the main thread and wait for it, use runBlocking
- it's really meant for this.oday
09/05/2022, 8:47 AModay
09/05/2022, 8:47 AModay
09/05/2022, 8:52 AModay
09/05/2022, 8:57 AMThis 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.
oday
09/05/2022, 8:57 AMJoffrey
09/05/2022, 9:32 AMDispatchers.Main
doesn't have a default implementation without extra modules. You either need android, or javafx, or swing - something that brings some kind of UI thread that is used in the Main dispatcher. So they must be cheating and provide one of those for it to work on the website.
Secondly, GlobalScope
is generally discouraged unless you need a coroutine that runs for the whole life of the application, or if you want to manage the lifecycle of the coroutine manually (and know what you're doing). So it should definitely not be in a hello world example for beginners.oday
09/05/2022, 9:41 AModay
09/05/2022, 7:35 PMThis executes immediately
is fired …immediately and then the coroutine inside the runBlocking runsoday
09/05/2022, 7:35 PMexecutes immediately
oday
09/05/2022, 9:13 PMfun main() {
GlobalScope.launch {
stallForTime()
println("This is executed after the delay")
}
println("This is executed immediately")
Thread.sleep(3000)
}
Joffrey
09/05/2022, 9:27 PMMain
dispatcher defined (tied to the JS event loop), and maybe this executes all enqueued tasks before terminating the program (that wouldn't surprise me)