How do you get ktor suspend function working insid...
# javascript
d
How do you get ktor suspend function working inside of application that uses JSExport. I have an example JsExport repo here https://github.com/DavidCorrado/KMPWithWeb/blob/main/shared/src/jsMain/kotlin/com/example/kmpwithweb/PokemonList2ViewModel.kt
I can get the promise to work but not the commented code here which is a ktor request which is a suspend function. I assume there is just something small here I am not understanding correctly. I know directly you are not allowed to use a suspend function in jsexport but indirectly too? Anyway to get a ktor request with jsexport annotation?
g
What's the error? Is it at compilation? Or an exception is thrown in the ViewModel and it's not caught?
h
You can't expose a suspend function with JsExport, but
scope.promise { }
and expose
Promise
g
The code looks good to me,
GlobalScope.promise{ }
is used and it exposes a
Promise
. (Note that you may have to remove
kotlin.js.
(package name) in the Typescript generated for Promise.)
d
TypeError: Cannot read properties of undefined (reading 'getList_umzkcw_k$')
Screen Shot 2022-05-10 at 3.33.22 PM.png
I am using just pure JS. Got the above error running in chome developer tools
d
Curious to see what is failing to be read. If you are building with webpack you can import the mappings or build without webpack. You can also call the functions directly through the console to help debug.
if you are running in chrome you can open up the developer tools and it will show you which line is throwing that exception which will help debugging
also, your code works for me
Screen Shot 2022-05-10 at 4.45.41 PM.png
was running this task
:webApp:browserDevelopmentRun
d
The code is not running the coroutine. If you comment the getList and use the commented line you get the error
I dont think I am running webpack. Running the same task. Also I included a screenshot of the error from chrome developer tools manually ran. I am not able trace the code when it starts getting inside the coroutine I get confused as the code gets super complicated
d
When I run it, it looks like the async method is missing
checking one thing
for some reason the function is undefined
Screen Shot 2022-05-10 at 4.58.03 PM.png
d
webApp.com.example.kmpwithweb.PokemonList2ViewModel.prototype.getListAsync() is defined for me
In the generated share JS it seems to be failing on suspendResult = this.this$0__1.pokemonListViewModel_1.getList_umzkcw_k$(this); In specific this$0__1 seems to be correct but pokemonListViewModel_1 is not defined which it does not have a JSExport defined but it cant because there is a suspend function there
d
For some reason this works
Copy code
println("The model is ${PokemonListViewModel()}")
        println("The list is ${PokemonListViewModel().getList()}")
        println("The list is ${PokemonListViewModel().getList().toTypedArray()}")
Screen Shot 2022-05-11 at 9.54.11 AM.png
But if we try to use the property that was instantiated in the class it doesn't work
Copy code
private val pokemonListViewModel = PokemonListViewModel()
g
Oh! Then it may be due to the initialization order. You may want to pass it in the constructor instead so that it's initialized before everything else.
(or you could try to define it with a
by lazy { ... }
if ever it works for the compiler...)
d
by lazy
had the same issue with the viewmodel being undefined when we used it