Hey folks, I'm rather new to Kotlin JS, trying to ...
# javascript
j
Hey folks, I'm rather new to Kotlin JS, trying to import
async
functions into Kotlin, some pointers would be greatly appreciated because I'm early missing something (https://stackoverflow.com/questions/71678834/how-do-i-import-and-use-an-async-js-function-in-kotlin-js)
s
suspend
keyword won't work in
external declaration
. I think you want
SignUp
function to return a
Promise<String>
external object
are considered as pure JS objects and JS doesn't have
suspend
.
j
oh right, well that would explain πŸ˜„
LEt me try that
b
async
in JS is just a syntactic sugar for a function returning Promise to unwrap it to callbacks. So
async function myFun(): Promise<String>
would be represented in kotlin as
external fun myFun(): kotlin.js.Promise<String>
From there, you can use coroutines runtime lib utilities to convert the promises to suspend scopes and other coroutine builders.
βž• 1
j
Right, @Big Chungus that's kind of what I was trying and indeed now I get objects instead of Promises back (after adding
.await()
in my coroutine. But somehow I can't unpack the object so I'm wondering if it's translated correctly
Alright, turns out suspend works just find @shaktiman_droid. I indeed had to wrap my external into Promise<> and use .await() inside a coroutine. TY @Big Chungus!
πŸ‘ 1
s
having
suspend
and return a
Promise
would not make sense.
suspend
might not be doing anything since you are using
await
inside
coroutine
b
You might've seen this already, but I've covered most of the things one needs to know about wrapping js in kotlin here
j
You referred me to it last time I had an issue. I'll go back to it haha. Thnaks!
Right, I got it mixed up. Indeed @shaktiman_droid suspend doesn't make sense any more here
πŸ‘πŸ½ 1
b
It didn't have async section last time. Just added it now for completeness πŸ™‚
βž• 2
j
@Big Chungus Could you add the imports used in the Kotlin examples of your post? I find it extremely helpful to have these 😁
πŸ‘ 1
b
Hmm, most of the snippets only use global APIs, but I'll review and add imports where appropriate (surely skipping an import of kotlin.String is no big deal) πŸ˜€
j
For sure, but for example await from kotlinx would be nice to have the imports for ✌️
πŸ‘ 1