From 993718531cc9a7d6947777e0a52eab62c3278f2d Mon Sep 17 00:00:00 2001 From: Alex Shepeliev Date: Thu, 2 May 2024 16:14:48 +0300 Subject: [PATCH 1/2] Add test --- kotlinx-coroutines-core/wasmJs/test/PromiseTest.kt | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/kotlinx-coroutines-core/wasmJs/test/PromiseTest.kt b/kotlinx-coroutines-core/wasmJs/test/PromiseTest.kt index 63550439eb..d73716a58d 100644 --- a/kotlinx-coroutines-core/wasmJs/test/PromiseTest.kt +++ b/kotlinx-coroutines-core/wasmJs/test/PromiseTest.kt @@ -84,4 +84,15 @@ class PromiseTest : TestBase() { null } } + + @Test + fun testAwaitPromiseRejectedFromJs() = GlobalScope.promise { + lateinit var r: (JsAny) -> Unit + val toAwait = Promise { _, reject -> r = reject } + val throwable = async(start = CoroutineStart.UNDISPATCHED) { + assertFails { toAwait.await() } + } + r("Rejected".toJsString()) + assertContains(throwable.await().message ?: "", "Rejected") + } } From 0f22826df318016607b10bbe5d394acccc985096 Mon Sep 17 00:00:00 2001 From: Alex Shepeliev Date: Thu, 2 May 2024 16:17:42 +0300 Subject: [PATCH 2/2] Wrap JS exception instead of throwing IllegalStateException on promise rejection --- kotlinx-coroutines-core/wasmJs/src/Promise.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kotlinx-coroutines-core/wasmJs/src/Promise.kt b/kotlinx-coroutines-core/wasmJs/src/Promise.kt index 9099a7c3e9..6149de9616 100644 --- a/kotlinx-coroutines-core/wasmJs/src/Promise.kt +++ b/kotlinx-coroutines-core/wasmJs/src/Promise.kt @@ -75,6 +75,6 @@ public fun Promise.asDeferred(): Deferred { public suspend fun Promise.await(): T = suspendCancellableCoroutine { cont: CancellableContinuation -> this@await.then( onFulfilled = { cont.resume(it as T); null }, - onRejected = { cont.resumeWithException(it.toThrowableOrNull() ?: error("Unexpected non-Kotlin exception $it")); null } + onRejected = { cont.resumeWithException(it.toThrowableOrNull() ?: Exception("Non-Kotlin exception $it")); null } ) }