How to properly call `suspend` function from `js` ...
# javascript
t
How to properly call
suspend
function from
js
site? or are we shouldn’t call at all?
g
what is “properly call”? You should call suspend function from any coroutine builder, like
launch
👍 1
j
I believe @Tuan Kiet wants to call a Kotlin JS suspend function from JavaScript code, not from Kotlin JS code, but maybe I misunderstood
t
that true, and I quickly realize that from java (or any platform) you shouldn’t call any suspend function, just call a regular function from a object that perform
launch
of
async
with it own scope
👍 1
j
Indeed, suspend functions are a Kotlin feature, and are not really designed to be used from platform code AFAIK
s
I only found an ugly wokaround with JS promises which looks like this for my function.
Copy code
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.promise
import kotlin.js.Promise

suspend fun getGroup(…): Group = …

@JsName("getGroup")
fun getGroupWrapper(…): Promise<Group> =
    GlobalScope.promise { 
        getGroup(…) // suspend fun getGroup(): Group 
    }
g
It's not ugly, it's way how you make coroutines work with platform asyncronous API, the same on JVM
👌 2