I can't figure out why I'm getting this ClassCastE...
# compose-web
b
I can't figure out why I'm getting this ClassCastException on await():
Copy code
external interface BwipJs {}
external fun <Module : Any> import(path: String): Promise<Module>

@Composable
fun Barcode() {
  val bwip = remember { mutableStateOf<BwipJs?>(null) }
  LaunchedEffect(Unit) {
    bwip.value = import<BwipJs>("bwip-js").await() // ClassCastException
  }
}
Outside of Compose, in plain Kotlin/JS, the import works fine, and I'm successfully calling functions (rendering barcodes) using the BwipJs handle, e.g.
Copy code
val canvas = document.createElement("canvas") as HTMLCanvasElement
import<BwipJs>("bwip-js").then { bwipjs ->
    bwipjs.toCanvas(canvas)
}
a
This isn’t compose web issue, this is Kotlin/JS issue.
b
Do you know what the issue is? I was also able to reproduce it via just coroutines, so I moved my bug report over there: https://github.com/Kotlin/kotlinx.coroutines/issues/3009
a
It most liekly there’s issue in Kotlin wrapper of
Promise
, you can easily write your own version of
await()
using `then`/`catch` methods of
Promise
and coroutines API which will work corretcly.
Actually i had done same workaround in one place and forgot to report it to Kotlin team. Thanks for report!
b
Hmm, I think I tried every combination of await/then/catch and they all had the same issue. I suspect it's a bug deeper in the coroutines machinery.