1 year ago
    I have a Kotlin Multiplatform library I'm trying to use from
    as a npm package. I can call normal functions just fine in JS, but
    functions are a bit of an enigma from the
    side. I know I can create a wrapper in
    but I'd rather not manually do that for every suspend function. Is there any way to call a suspend function in JS and get a promise back (even if it requires a little helper function?) Here is a modified version of the Base64 library example to show what I'm trying to do. Kotlin Implementation
    interface Base64Encoder {
        fun encode(src: ByteArray): ByteArray
        fun encodeToString(src: ByteArray): String {
            return encode(src).let { encoded ->
                buildString(encoded.size) {
                    encoded.forEach { append(it.toChar()) }
        suspend fun downloadAndEncodeToString(url: String): String {
            return HttpClient().use {
                it.get<HttpResponse>(url = Url(url)).readText()
    JS Usage (see image below)


    1 year ago
    It seems like you basically want to do whatever kotlinx.coroutines.promise does - if you can simply call that, that’s easiest. Otherwise, you basically need to call the suspend function with a continuation that will call resolve/reject and “false” as the suspended flag.