diff --git a/IntegrationTests/TestSuites/Sources/ConcurrencyTests/main.swift b/IntegrationTests/TestSuites/Sources/ConcurrencyTests/main.swift index 3bce2aa8d..1e48f459f 100644 --- a/IntegrationTests/TestSuites/Sources/ConcurrencyTests/main.swift +++ b/IntegrationTests/TestSuites/Sources/ConcurrencyTests/main.swift @@ -32,6 +32,7 @@ func entrypoint() async throws { resolve(.success(1)) }) try await expectEqual(p.value, 1) + try await expectEqual(p.result, .success(.number(1))) } try await asyncTest("await rejected Promise") { @@ -41,6 +42,7 @@ func entrypoint() async throws { let error = try await expectAsyncThrow(await p.value) let jsValue = try expectCast(error, to: JSValue.self) try expectEqual(jsValue, 3) + try await expectEqual(p.result, .failure(.number(3))) } try await asyncTest("Continuation") { diff --git a/Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift b/Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift index 53008c5e4..c6d10209a 100644 --- a/Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift +++ b/Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift @@ -116,6 +116,24 @@ public extension JSPromise { } } } + + /// Wait for the promise to complete, returning its result or exception as a Result. + var result: Result { + get async { + await withUnsafeContinuation { [self] continuation in + self.then( + success: { + continuation.resume(returning: .success($0)) + return JSValue.undefined + }, + failure: { + continuation.resume(returning: .failure($0)) + return JSValue.undefined + } + ) + } + } + } } #endif