Skip to content

Commit

Permalink
add another initializer for Promise to improve concurrency bridge (#60)
Browse files Browse the repository at this point in the history
Co-authored-by: Oguz Yuksel <[email protected]>
  • Loading branch information
OguzYuuksel and Oguz Yuksel authored Oct 25, 2024
1 parent 4ade9d4 commit 76b7b80
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Sources/FoundationExtensions/Promise/Promise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,28 @@ extension Publishers {
}
}

@available(macOS 14.0, iOS 17.0, tvOS 17.0, watchOS 8.0, *)
public init<InnerFailure: Error>(_ asyncPromise: @escaping @Sendable () async -> Publishers.Promise<Success, InnerFailure>) where Failure == Error, Success: Sendable {
self.init { promise in
// FIXME: https://forums.swift.org/t/promise-is-non-sendable-type/68383
// Apple does not really care about Combine framework anymore...
#if swift(>=6)
nonisolated(unsafe) let promise = promise
#endif
let task = Task { @Sendable in
let innerPromise = await asyncPromise()
do {
let result = try await innerPromise.value()
promise(.success(result))
} catch {
promise(.failure(error))
}
}

return AnyCancellable { task.cancel() }
}
}

/// This function is called to attach the specified `Subscriber` to this `Publisher` by `subscribe(_:)`
///
/// - SeeAlso: `subscribe(_:)`
Expand Down

0 comments on commit 76b7b80

Please sign in to comment.