Skip to content

Commit 901de66

Browse files
Add async JSPromise.result property (#200)
1 parent f1ef517 commit 901de66

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

IntegrationTests/TestSuites/Sources/ConcurrencyTests/main.swift

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func entrypoint() async throws {
3232
resolve(.success(1))
3333
})
3434
try await expectEqual(p.value, 1)
35+
try await expectEqual(p.result, .success(.number(1)))
3536
}
3637

3738
try await asyncTest("await rejected Promise") {
@@ -41,6 +42,7 @@ func entrypoint() async throws {
4142
let error = try await expectAsyncThrow(await p.value)
4243
let jsValue = try expectCast(error, to: JSValue.self)
4344
try expectEqual(jsValue, 3)
45+
try await expectEqual(p.result, .failure(.number(3)))
4446
}
4547

4648
try await asyncTest("Continuation") {

Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift

+18
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,24 @@ public extension JSPromise {
116116
}
117117
}
118118
}
119+
120+
/// Wait for the promise to complete, returning its result or exception as a Result.
121+
var result: Result<JSValue, JSValue> {
122+
get async {
123+
await withUnsafeContinuation { [self] continuation in
124+
self.then(
125+
success: {
126+
continuation.resume(returning: .success($0))
127+
return JSValue.undefined
128+
},
129+
failure: {
130+
continuation.resume(returning: .failure($0))
131+
return JSValue.undefined
132+
}
133+
)
134+
}
135+
}
136+
}
119137
}
120138

121139
#endif

0 commit comments

Comments
 (0)