Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

106
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda