Sebastian Faltoni Software Architect. Passionate about Clean Code, Tech, Web, AI, .NET, Python, IOT, Swift, SwiftUI and also Blazor

A simple extension to use Combine with PromiseKit and ServiceStack on Swift

39 sec read

Because ServiceStack use PromiseKit for it’s async calls on Swift, i made this little extension that convert a PromiseKit request into a Combine publisher.

 extension Promise {
     var publisher : AnyPublisher<T, Error> {
         Future<T, Error> {
             promise in
             self.done {
                 body in
                 promise(.success(body))
             }.catch {
                 error in
                 promise(.failure(error))
             }
         }.eraseToAnyPublisher()
     }
 } 

Let’s say you have a ServiceStack Client witch return a Promise you can do something like the following.

let rq = GetListings()
client.getAsync(rq)
             .publisher
             .map {
                 response in
                 response.listings
             }
.... 

Sebastian Faltoni Software Architect. Passionate about Clean Code, Tech, Web, AI, .NET, Python, IOT, Swift, SwiftUI and also Blazor

Leave a Reply

Your email address will not be published. Required fields are marked *