2

I'm having issues importing a Javascript async function into my Kotlin code.

Here is an example Javascript function :

export async function signUp(email, password){

    console.log("Signing Up");

    try {
        const userCred = await createUserWithEmailAndPassword(auth, email, password);
        console.log(userCred);

        return userCred.user.accessToken
    }
    catch (e) {
        console.log(e);
    }

}

I import it in my Kotlin as such :

@JsModule("@jlengrand/firebase-ports")
@JsNonModule
external object FirebasePorts{

    suspend fun signUp(email: String, password: String) : String
}

When using the function, I expect to get a String out of it :

    var user : String? by mutableStateOf(null)

            Button( attrs = {
                onClick { 
                    GlobalScope.launch {
                        user = FirebasePorts.signUp(email, password)
                    }
                }
            }) {
                Text("Login!")
            }

            // printing the value
            TextArea(value = user.toString())

However, what I get instead is a Promise

enter image description here

What am I doing wrong?

Thanks!

1 Answer 1

3

Async functions in JS are currently not mapped to suspend functions in Kotlin. To get an async function working in Kotlin, you need to deal with the native promises:

import kotlin.js.Promise

@JsModule("@jlengrand/firebase-ports")
@JsNonModule
external object FirebasePorts {

   fun signUp(email: String, password: String) : Promise<String>
}

Then use FirebasePorts.signUp(...).then { ... } to access the value.

Sign up to request clarification or add additional context in comments.

1 Comment

Yup, that works thanks! Expecting suspend to be mapped to async was clearly a stretch. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.