When exporting a kotlin interface with suspending ...
# javascript
a
When exporting a kotlin interface with suspending functions im getting the following error:
Declaration of such kind (suspend function) cant be exported to JS
How do i get around this? im currently marking my interface as external to be able to export it to JS, but how can i export a suspending function? Here is an example:
Copy code
@JsExport
@JsName("CoolInterface")
external interface CoolInterface {

    @JsName("suspendingFunc")
    suspend fun suspendingFunc(root: String): String?
}
m
You'll need to expose a Promise I believe
a
yes but that is tricky when this is a KMP project, so my JVM layer wants to be a suspending function
m
If the caller is Kotlin then you don't need
JsExport
?
If the caller is JS then it doesn't know anything about
suspend
Same for Swift where
suspend
are generated as functions with completions
a
this is a library written in KMP, supports JS and JVM targets, the interface is required to be exported as it is implemented in the consuming platform (e.g. JS and JVM)
m
But it's going to be implemented in Kotlin, right?
What happens if you remove
@JsExport
?
a
if i dont add a jsExport, then my native JS code will not see the interface
m
Mmmmm the interface will be implemented in Kotlin, not JS, right?
a
no
m
Ah
a
yep
🤣
m
yea well I don't think that's possible
a
i was thinking something on the lines of being able to do an expect
but i think it wont work
If i use legacy, it will work, but im trying to upgrade my project to IR and 1.6
h
Current workaround is creating a wrapper to return the promise on JS: https://kotlinlang.slack.com/archives/C0B8L3U69/p1634577714266300
a
If you want to implement the interface in JS/iOS, you could try to export a callback-based Interface instead, and then have a way to convert it to a suspend fun within your KMP project