Search code examples
swiftpromisekitreactive-swift

Converting PromiseKit to Signal & SignalProducer


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.


Solution

  • 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)
                }
            }
        }
    
    }