1

I'm trying to convert a Promise<T> from PromiseKit into a ReactiveSwift SignalProducer but I'm having trouble coming up with it. Could someone point me in the right direction?

Currently I have:

extension SignalProducer {
    func from(promise: Promise<Value>) -> SignalProducer<Value, Error> {
        return SignalProducer { observer, disposable in
            promise.then {
                observer.send(value: $0)
                observer.sendCompleted()
            }.`catch` { error in
                observer.send(error: error)
            }
        }
    }
}

I'm trying to emulate rxjs' fromPromise method.

barndog
  • 6,975
  • 8
  • 53
  • 105
  • This is something that I have had trouble doing myself. it seems as though the nature of Promisekit is quite mysterious. – Tom Testicool Apr 16 '17 at 22:20

1 Answers1

1

This is what I came up with.

extension SignalProducer where SignalProducer.Error: Swift.Error {

    static func from(promise: Promise<Value>) -> SignalProducer<Value, Error> {
        return SignalProducer { (observer: Observer<Value, Error>, disposable: Disposable) in
            promise.then { value -> () in
                observer.send(value: value)
                observer.sendCompleted()
            }.catch { (error: Swift.Error) -> Void in
                observer.send(error: error as! Error)
            }
        }
    }

}
barndog
  • 6,975
  • 8
  • 53
  • 105