I added JS target to my KMP project and now sudden...
# multiplatform
v
I added JS target to my KMP project and now suddenly I cannot use
<http://Dispatchers.IO|Dispatchers.IO>
in commonMain It was there when the project only targeting android and iOS and not
js(IR)
m
You can only use functions and properties in commonMain that are available on all the targets you are using. JS does not have a
<http://Dispatcher.IO|Dispatcher.IO>
so you cannot use it. Instead you will have to create some expect function or property that will return the dispatcher you want to use for IO.
v
Which dispatcher to use for JS?
m
Would be a nice things for Kotlin to just include by default, even if JS returns the same dispatcher for every value in Dispatchers.
I haven't used JS, but I think they only have one since they don't support threads.
I would probably go with Default, it's the closest to IO.
v
Thanks, even runBlocking is not in JS
m
yep
v
Any idea on how runBlocking can be achieved on JS side?
m
There's been a lot of discussion on it in the past, but you could look at how
runTest
is implemented in JS.
v
runTest is not in JS
m
I thought it was, but I've never used js
v
No issues, thanks for helping
m
This imp[lies that it is available
On JVM and Native, this function behaves similarly to
runBlocking
, with the difference that the code that it runs will skip delays. This allows to use delay in tests without causing them to take more time than necessary. On JS, this function creates a
Promise
that executes the test body with the delay-skipping behavior.
https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/run-test.html
v
My use case is a bit different
Copy code
expect fun <T> runBlocking(
    context: CoroutineContext = EmptyCoroutineContext, 
    block: suspend CoroutineScope.() -> T
): T
This returns the data of type T which is what I want I tried using GlobalScope, but it returns a Job or a Deferred
m
I think if you are going to support JS you will need to be able to wait for the answer in a non blocking manner. Since there is only one thread in JavaScript, you cannot block that thread.