Skip to content

Commit 15af3c8

Browse files
Add JSPromise.value() method to wto avoid switching isolation domains
Otherwise, we don7t have a way to wait for a promise created on a worker to complete.
1 parent 7790846 commit 15af3c8

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Diff for: Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift

+18
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,24 @@ public extension JSPromise {
232232
}
233233
}
234234

235+
/// Wait for the promise to complete, returning its result or exception as a Result.
236+
///
237+
/// - Note: Calling this function does not switch from the caller's isolation domain.
238+
func value(isolation: isolated (any Actor)? = #isolation) async throws -> JSValue {
239+
try await withUnsafeThrowingContinuation(isolation: isolation) { [self] continuation in
240+
self.then(
241+
success: {
242+
continuation.resume(returning: $0)
243+
return JSValue.undefined
244+
},
245+
failure: {
246+
continuation.resume(throwing: JSException($0))
247+
return JSValue.undefined
248+
}
249+
)
250+
}
251+
}
252+
235253
/// Wait for the promise to complete, returning its result or exception as a Result.
236254
var result: JSPromise.Result {
237255
get async {

0 commit comments

Comments
 (0)