albertosh
07/08/2020, 3:22 PMexpect class Wrapper<T: Any?>
Working Kotlin for JVM code
actual typealias Wrapper<T> = CompletableFuture<T>
Not working Kotlin for JS code
actual typealias Wrapper<T> = Promise<T>
Compiler claims that
Aliased class should not have type parameters with declaration-site variance
Any clue?Big Chungus
07/08/2020, 4:00 PMrusshwolf
07/08/2020, 4:30 PMopen class Promise<out T>
and the out
and open
are missing in your expect
statement.albertosh
07/09/2020, 8:58 AMinternal expect fun asyncWrapper(body: suspend () -> ExperimentJournal?): AsyncWrapper<ExperimentJournal?>
expect class AsyncWrapper<T: Any?>
JVM code
internal actual fun asyncWrapper(body: suspend () -> MyClass?): AsyncWrapper<MyClass?> =
GlobalScope.future {
body()
}
actual typealias AsyncWrapper<T> = CompletableFuture<T>
JS code
internal actual fun asyncWrapper(body: suspend () -> MyClass?): AsyncWrapper<MyClass?> {
return AsyncWrapper(GlobalScope.promise {
body()
})
}
actual class AsyncWrapper<T : Any?>(
internal val promise: Promise<T>
) {
@JsName("then")
fun <S> then(onFulfilled: ((T) -> S)?, onRejected: ((Throwable) -> S)?): Promise<S> = promise.then(onFulfilled, onRejected)
@JsName("catch")
fun <S> catch(onRejected: (Throwable) -> S): Promise<S> = promise.catch(onRejected)
}
suspend fun AsyncWrapper<Any>.await() = promise.await()
I don’t actually like much the JS one. I tried to extend Promise but I kept getting some undefined related errors, so I had to replicate some code. In the end, it’s not that much code to replicatealbertosh
07/09/2020, 8:59 AMCompletableFuture
and in JS as Promise
It’s just an “output” type of the library, so it will not be used internally