Does the functionality of "launch" not work the sa...
# multiplatform
j
Does the functionality of "launch" not work the same on JS clients as JVM clients? When I run the following code snippet in JS, "isActive" will never return false and thus the code in GlobalScope.launch is never run "first" or never as an asynchronous job. How are we suppose to write asynchronous code that will compile down for java and javascript?
Copy code
fun doLaunchOperation() {
        println("START: doLaunchOperation()")
        var i = 0;
        val launchJob = GlobalScope.launch {
            println("START: launch")
            while (i<100){
                println(i)
                i++
            }
            println("END: launch")
        }

        while(launchJob.isActive){
            println("launchJob.isActive")
        }

        println("done")
        println("END: doLaunchOperation()")
    }
e
JS is single-threaded so your
while
loop will never see any progress made
same as Javascript itself: you cannot bridge asynchronous code to synchronous code
j
Is that a defect? Why would I write multiplatform code that works one way with the JVM target and another way with the JS target? Doesn't seem very multiplatform...
e
that is how JS works, there is no way around it.
Kotlin multiplatform doesn't magically free you from the platform's constraints
also even though JVM and native support threading, what you have done there is not good either. instead of pinning a thread in a busy loop, you should use
runBlocking { }
to bridge from async to sync. this function is absent in Kotlin/JS, due to the fact that it is impossible.
you can write multiplatform asynchronous code; the JS limitation only means that you cannot wait for it in multiplatform synchronous code. for example,
Copy code
suspend fun main() {
    val job = GlobalScope.launch {
        for (i in 1..100) {
            println(i)
            delay(100)
        }
    }
    job.join()
}
works on all platforms, including JS
j
That does not work, because if you use job.join() you need to make the function suspend and suspend functions cannot be exported to JS. Any other ideas?
e
there are functions to bridge coroutines to JS Promise
you cannot export a blocking function to JS, but you can export a function which returns a Promise
j
Thanks for your suggestion, I'll try to use a promise, but that will probably make the java side look ugly, but we'll see.
e
JS Promise is not available for other platforms
if you want to export a suspending function to non-Kotlin code, you will need different APIs for different platforms
j
That's my frustration with this multiplatform stuff... it doesn't support async operations... at least I haven't figured out how to write multiplatform code that works the same in JVM and JS.
e
the underlying platforms don't have anything even close to the same async capabilities, what do you expect Kotlin to do about it? either you stay fully within the Kotlin world, in which case everything is can be handled by Kotlin, or you need to write platform-specific integrations because the platforms are different.
j
Well I would at least expect a promise would work that same for both JVM and JS, they both support a Promise... Both JVM and JS support asynchronous operations and code.
e
JS promise and JVM future don't have much in common though. if you can write useful common interface I'm sure your contribution would be useful