Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
Using JS function returning dynamic in Kotlin JS within the default Promise constructor results in ClassCastException

I got this error when trying to transform gapi calls into Promises, but I suppose calls to other functions may result in the same issue. First, I added the library as a script in the page:

script(src = "https://apis.google.com/js/platform.js") {
    async = true
    defer = true
}

next, I fetched gapi object from window:

import kotlinx.browser.window
import org.w3c.dom.get

val gapi = window["gapi"]

finally, I used the load function like so:

import kotlin.js.Promise

fun loadGapi() = Promise<Unit> { resolve, _ ->
    gapi.load("client:auth2") { resolve(Unit) }
}

giving me a warning in the IDE:

IDE warning: Implicit (unsafe) cast from dynamic to Unit

This resulted in the following error when called inside a page:

Uncaught (in promise) ClassCastException {message: undefined, cause: undefined, name: 'ClassCastException', stack: 'ClassCastException\n at THROW_CCE (http://localh… (http://localhost:3000/static/app.js:4930:14)'}

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Not sure why this happens. I tried reproducing the issue with evaluated JS code to no avail:

import kotlin.js.Promise

fun loadGapi(): Promise<Any> {
    val functionReturningUndefined =
        js("function (resolve) { resolve('test'); return undefined; }")
    return Promise { resolve, _ ->
        functionReturningUndefined { param -> resolve(param as Any) } as Unit
    }
}

Anyways, the problem can be resolved by ignoring the return value of the function and adding return@Promise Unit like so:

fun loadGapi() = Promise<Unit> { resolve, _ ->
    gapi.load("client:auth2") { resolve(Unit) }
    return@Promise Unit
}

Alternatively, an extension function may be added (I put mine in shared/Promises.kt) that specifically targets the dynamic return type:

fun <T> Promise(executor: (resolve: (T) -> Unit, reject: (Throwable) -> Unit) -> dynamic) =
    Promise<T> { resolve, reject ->
        executor(resolve, reject)
        Unit
    }

then simply:

import shared.Promise

fun loadGapi() = Promise<Unit> { resolve, _ ->
    gapi.load("client:auth2") { resolve(Unit) }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!