Why is this function (coroutines) ```@Suppress("UN...
# coroutines
p
Why is this function (coroutines)
Copy code
@Suppress("UNCHECKED_CAST")
public suspend fun <T> Promise<JsAny?>.await(): T = suspendCancellableCoroutine { cont: CancellableContinuation<T> ->
    this@await.then(
        onFulfilled = { cont.resume(it as T); null },
        onRejected = { cont.resumeWithException(it.toThrowableOrNull() ?: error("Unexpected non-Kotlin exception $it")); null }
    )
}
Not implemented with type safely like? Also why does it use a cancellable coroutine if the continuation never gets cancelled? Is there something I don’t see?
Copy code
suspend fun <T : JsAny?> Promise<T>.await(): T = suspendCoroutine { cont: Continuation<T> ->
    this@await.then(
        onFulfilled = {
            cont.resume(it)
            null
        },
        onRejected = {
            cont.resumeWithException(
                it.toThrowableOrNull()
                    ?: error("Unexpected non-Kotlin exception $it")
            )
            null
        }
    )
}
b
For 2nd q. - i guess promises are not cancelable? In this case it still makes sense to use suspendCancelableCoroutine, because suspendCancelableCoroutine vs suspendCoroutine is not only about cancellation underlying callback (promise in this case) but also about cooperativeness for the caller. For example, let's say you have a promise that returns a value after 1hr delay. Now you invoked promise.await() in some scope, but after waiting for 1min that coroutine gets cancelled. With suspendCoroutine it will wait for promise' completion (1hr) before that scope will be able to be properly cancelled