How does one handle this? Specifically, how to dea...
# javascript
n
How does one handle this? Specifically, how to deal with the coroutine? At minimum I imagine I'll have to serialize the data to pass it.
Copy code
// FIXME
    @JsExport fun fetchCountriesJs() : List<Country> {
        return fetchCountries()
    }

    suspend fun fetchCountries() : List<Country> {
        val countries: List<Country>? = store.get()
        if (countries.isNullOrEmpty()) return api.fetchCountries().also { store.set(it) }
        return countries
    }
e
JS can't call `suspend fun`s directly but you can create a promise in Kotlin and await it in JS. also converting it to a JsArray will make it easier to consume in JS
Copy code
@JsExport
fun fetchCountriesJs() = GlobalScope.promise {
    fetchCountries().map {
        // convert Country to something usable by JS
    }.asJsReadonlyArrayView()
}
K 1
e
You don't need
asJsReadonlyArrayView
btw. Since 2.0 you should be able to export Kotlin collections directly, and use the view on the JS side.
👍 1
If you don't want to manually provide an exportable function, check out https://github.com/ForteScarlet/kotlin-suspend-transform-compiler-plugin
The more people using it, the better it will become.