Raman Sharma
Raman Sharma

Reputation: 177

How to handle suspend functions in Kotlin Multiplatform targeting JS?

We are developing a Kotlin Multiplatform SDK that uses Ktor as the network client to handle all network calls and authentication. This SDK is intended for use across Android, iOS, and web applications.

So far, we have successfully implemented the SDK using Kotlin Multiplatform and Ktor for managing network calls and authentication. We have also implemented several suspend functions to handle network calls and have successfully consumed the SDK on both Android and iOS platforms.

However, we are facing an issue when trying to export the SDK for JavaScript. Specifically, we have encountered a problem where suspend methods cannot be exported for JavaScript.

Here is a sample class which I want to export to JS:

class DocumentRepository(private val documentUseCase:GetDocumentUseCase) {

    suspend fun getDocumentById(docId:String): String? {
        var docContent:String? = null
        documentUseCase.getDocumentById(docId = docId).onSuccess {
            docContent = it
        }.onError {
            docContent = null
        }
        return docContent
    }
}

As per JetBrains team, export of suspend function is not yet supported. Here are related issues with more info: https://youtrack.jetbrains.com/issue/KT-56281/KJS-Cant-export-suspend-functions

https://youtrack.jetbrains.com/issue/KT-32619/JS-return-Promise-when-continuation-is-not-provided

Upvotes: 1

Views: 161

Answers (0)

Related Questions