如何在 Kotlin/JS 中导入和使用异步 JS 函数?
我在将 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
What am I doing wrong?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JS中的异步函数目前尚未映射以暂停Kotlin中的功能。要获得在Kotlin工作的异步功能,您需要处理本机承诺:
然后使用
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:
Then use
FirebasePorts.signUp(...).then { ... }
to access the value.