Make Future from Publisher

Updated
Jun 20, 2020 5:26 PM
Created
Jun 20, 2020 5:25 PM
Tags
Combine
Keywords
Date

import Combine

extension Publisher {

  @discardableResult
  func start() -> Future<Output, Failure> {
    return .init { promise in

      var cancellableRef: Unmanaged<AnyCancellable>? = nil

      let c = self.sink(receiveCompletion: { (completion) in
        switch completion {
        case .failure(let error):
          promise(.failure(error))
          cancellableRef?.release()
        case .finished:
          break
        }
      }) { (value) in
        promise(.success(value))
        cancellableRef?.release()
      }

      cancellableRef = Unmanaged.passRetained(c)
    }
  }
}