如何在 Kotlin/JS 中导入和使用异步 JS 函数?

发布于 2025-01-18 05:02:25 字数 1250 浏览 2 评论 0原文

我在将 Javascript 异步函数导入 Kotlin 代码时遇到问题。

这是一个示例 Javascript 函数:

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);
    }

}

我将其导入到 Kotlin 中:

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

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

使用该函数时,我希望从中获取一个字符串:

    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())

但是,我得到的是一个 Promise

在此处输入图像描述

什么是我在做 错误的?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初心未许 2025-01-25 05:02:25

JS中的异步函数目前尚未映射以暂停Kotlin中的功能。要获得在Kotlin工作的异步功能,您需要处理本机承诺:

import kotlin.js.Promise

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

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

然后使用firebaseports.signup(...)。然后{...}访问该值。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文