I write function 'asPromise()'
extension PrimitiveSequence {
public func asPromise() -> Promise<Element> {
var disposed: Disposable? = nil
return Promise<Element> { seal in
disposed = self.asObservable()
.subscribe { event in
switch event {
case .next(let element):
seal.fulfill(element)
case .error(let error):
seal.reject(error)
case .completed:
disposed?.dispose()
}
}
}
}}
but dispose is weird, I don't better idea. so If you have any better ideas, please let me know
thank you
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:
Did you know that the Combine API already has a Promise like type built in?