ocean
ocean

Reputation: 13

Use the 'asPromise ()' in the Observable RxSwift can be used in a PromiseKit Promise?

I write function 'asPromise()'

but dispose is weird, I don't better idea. so If you have any better ideas, please let me know

thank you

Upvotes: 1

Views: 322

Answers (1)

Daniel T.
Daniel T.

Reputation: 33967

There is no need to capture the disposable at all. When the Observable completes it will automatically dispose of all the resources. Here is the proper implementation:

extension PrimitiveSequence where Trait == SingleTrait { // will only work properly for Singles
    public func asPromise() -> Promise<Element> {
        Promise<Element> { seal in
            _ = subscribe { event in
                switch event {
                case let .success(element):
                    seal.fulfill(element)
                case let .failure(error):
                    seal.reject(error)
                }
            }
        }
    }
}

Did you know that the Combine API already has a Promise like type built in?

extension PrimitiveSequence where Trait == SingleTrait {
    public func asFuture() -> Future<Element, Error> {
        Future { fulfill in
            _ = self.subscribe(fulfill)
        }
    }
}

Upvotes: 0

Related Questions