what is the correct way to JsExport suspend funct...
# javascript
j
what is the correct way to JsExport suspend functions? this is not supported:
Copy code
@JsName("blah2")
suspend fun blah2() {
should I GlobalScope its insides or is there a cleaner way?
Copy code
@JsName("blah2")
fun blah2() {
   GlobalScope.launch {
      flow.collect {
         println(it)
      }     
   }
}
e
Copy code
fun jsBlah() = promise { kotlinSuspendBlah() }
j
oooh, that looks useful! is there any specific package I should include in my gradle build file? I have
Copy code
val coroutinesVersion = "1.6.0"
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
in my commonMain and that
promise
does not appear in the autocomplete I have
Copy code
import kotlinx.coroutines.promise
and
Copy code
CoroutineScope.promise {  }
but that promise is not resolving
maybe an IntelliJ bug, let met try the usual restarts and wipe cache etc
e
not clear to me where your code is, but it won't work in common sourceset because it's a js-only function
j
my code is in jsMain do I need to pass in params to promise? almost seems like it's resolving the wrong thing here
e
https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/promise.html it does take a scope as the receiver, so
GlobalScope.promise {}
would be fairly equivalent to
GlobalScope.launch {}
but returning a
Promise
that JS can work with
j
probably the wrong CoroutineScope that's importing or something, if I do it explicit like this, it resolves thanks for the help!
I've made my own one now which replicates the official one without the boilerplate:
Copy code
fun <T> promise(
   context: CoroutineContext = EmptyCoroutineContext,
   start: CoroutineStart = CoroutineStart.DEFAULT,
   block: suspend CoroutineScope.() -> T,
): Promise<T> = CoroutineScope(context).async(context, start, block).asPromise()
Now this works correctly:
Copy code
@JsName("doSomething")
fun doSomething() = promise {
   state.emit(null)
   ""
}